Add a function in models.py or managers.py - django

I'd like to send an activation link to a new registered user. Should I write my function in my models.py or managers.py?
It's always confused to me to know where put the function, even after reading the documentation.

None of them, models and managers are related to application data. Sending emails are related to the logic of your app: actions, decisions, answers ... so you should do this in a view.
If you need to save time, you can use Django Registration as #karthikr suggests to you, this app is a good wrapper for reaching this aim.
I've assumed you need to send the link in the moment that the user has registered, but if you want to do this in other moment you can use a scheduled task: a django cron, an external python process or ...; that is up to you: your porpuse, the design of your app.

This blog could help you understand the use of managers better.
I would put the activation link in managers, because it would be easier to manage the various activities around it - resend activation link, validation, etc. It could be done with models too, but managers make it more modular.
Django Registration is quitely widely used for registration - you could see how it is implemented there as well.

Yes , you can write your customized function in models.py but not sure about manage.py .
I have used signal method in models.py to for mailservice facility.

Related

Django: Last modified by and created by user automatic saving

The age-old question: How can I automatically save last_modifed_user in django models?
I found in several places this general process how to do it using thread local. I'm hesitant to simply implement it that way because I'm not entirely sure of the consequences it has and because all these posts are old.
Is using thread local still the "recommended" way of doing this in django 3? Or does django3 have a better options of doing it?
No, this hasn't changed. Simply because separation of concern is an architectural principle of MVC (model-view-controller), which is also how Django (model-view-template) and most web frameworks with ORM are architected. Models know nothing about the request, it's not available (and in many cases there isn't a request at all when a model is saved, think of management commands or regular tasks running in the background).
The alternative to thread local is to make sure you implement it yourself in the controller layer (view layer in Django):
Create a view mixin that you can mix with all the generic views that use the ModelFormMixin to save the user into the model (ModelFormMixin.form_valid()). Or combine it with a form mixin where the user is passed to the form (FormMixin.get_form_kwargs()) and saved when the form is saved (ModelForm.save()).
Create a ModelAdmin mixin that does the same when saving a model in the django admin site.
This of course means someone on your team may forget to do it when creating new views and forms. The link you posted contains an answer as to the advantages and disadvantages of using thread local.

Django app has multiple database and multiple user

I have written one Django cloud based app. This app will have multiple user and for them multiple database, so that their data should be separate and they can save only to same database.
1) How can we implement it
2) How to automatically one user from login page to assign the database to write on it.
I don't have a complete answer, since you do not give a lot of detail. But here are a couple ots that f hinDjango supports custom database router implementations. A database router is a class that helps django decide which database to use for a particular model. Unfortunately I don't think this mechanism is granular enough for your needs. You can also specify the database to use in your code by using using(name) queryset method and save(using=name) form of save() method for instances. Of course this also means that some features of Django are going to be unvailable to you, since you cannot always expect to have a user. Look at the docs here for more info
https://docs.djangoproject.com/en/dev/topics/db/multi-db/

Notification to user from admin

I am new in Django and don`t know the best way to realise next functionality in my project.
In my django project I have users with different roles. One of them is admin who can create project. I mean that I have model with fields (project_name, project_managers) which filled by admin. The managers of the project are users of the system, admin can choice some of them. After all how to show them notification in real time that they was managers of the current project. Is it makes by websockets or Jquery or something else. I need some ideas how to realise that with detail explanation if it possible.
You can use post_save signal to implement your notification functionality. This signal will be called once the model object is saved.

How can one use Django Postman to set up a back end messaging system like Facebook?

I've done research on Django Postman and it seems to be the most solid private user to user messaging platform out there. I've looked at the Django Postman documentation but it's very template orientated. For developers who use Django as a back end and only care about the views.py and urls.py, the documentation doesn't say much.
I did however find this: https://bitbucket.org/psam/django-postman/src/6ff9fdf9c33f7365a7235a789af2e47f47d9c4fa/postman/views.py?at=default
It seems pretty promising so I'm going to give it a try. My only issue is how can one set up the postman views in views.py and the urls in urls.py to create a messaging system similar to Facebook's?
(ie. A thread like messaging conversation system, a central inbox where all the messages come together from each user showing the last message from each user, messages in the inbox are sorted by conversation rather than the message, the time of the last message sent, allowing multiple recipients)
Below I've posted a picture of Facebook's messaging platform. This is what I am essentially trying to achieve with Django Postman.
Facebook Example http://screenshots.en.sftcdn.net/en/scrn/73000/73077/facebook-19-371x535.jpg
If you have any pointers, hints and ideas on how I can set up the views.py, I would greatly appreciate it! Thank You
I've run into this issue before.
You need to strictly override some of the views in there by clonning/forking the project and install it from your own location, because as you noted, postman is template-oriented because it's meant to only get the needed templates configured and a few settings. I mean, the backend is meant to work as is.
What you need to do is override stuff like:
Message model's recipient field to be a ManyToManyField
customize the views based on your needs and be careful with Message.replied_at
make sure you allow a user to reply to their own messages (by default, it was not allowed when I ran into this, not sure now)
Depending on your needs, maybe you'll want to override something else, but this is a good start. If you need it facebook-like, you'll need to use some push libraries as Pusher or Juggernaut, maybe you're interested in them also.
Good luck! :)

create an object using the form django

Thank you for looking into this!
I doing one of those django tutorials from their official website, creating a poll. The poll app and everything is working.
The problem is that after I have created authentication for the users to log in they should be able to create the polls, aka they're given the fields with questions/choices, fil it in and from that data(form) it should a new poll object into the db. I have set up everything, but I cannot figure out how do I write the view for this, as in how do I extract data from the form and add it all as a new poll.
I am using three models, as in tutorial: polls, choices and user (user isn't recognisable either, i mean in the model 'user' i have a variable name = models.ForeignKey(User), I was using django-registration to register the them, but that's not the main problem at the moment).
I hope I am more or less clear, if not, I will be glad to explain again:)
thanks, blargie-bla
I'd recommend you starting with generic views.
https://docs.djangoproject.com/en/1.3/topics/class-based-views/#decorating-class-based-views
https://docs.djangoproject.com/en/1.3/ref/class-based-views/#editing-views