post_save in the admin - django

I have a application that i exclusively use with the admin.
I would like to send an email every time that a new object is created/modified, from my search, it seems that using the post_save would be the best way.
Unfortunately the doc is not really clear on that...
Can someone explain me > maybe with an example ?
Thanks

There are two ways. Either override the save function or use signals.
See the following excellent posts:
Django Signals
Django Signals vs Save (linked to archive.org original removed)
For a simple similar example using save have a look at my previous answer:
Send an e-mail notification when a Django CharField is modified via the admin site

Related

Why use signals in Django?

I have read lots of documentation and articles about using signals in Django, but I cannot understand the concept.
What is the purpose of using signals in Django?
How does it work?
Please explain the concept of signals and how to use it in Django code.
The Django Signals is a strategy to allow decoupled applications to get notified when certain events occur. Let’s say you want to invalidate a cached page everytime a given model instance is updated, but there are several places in your code base that this model can be updated. You can do that using signals, hooking some pieces of code to be executed everytime this specific model’s save method is trigged.
Another common use case is when you have extended the Custom Django User by using the Profile strategy through a one-to-one relationship. What we usually do is use a “signal dispatcher” to listen for the User’s post_save event to also update the Profile instance as well.

Where is the best place to send email in django-rest-framework?

I want to send emails in django rest framework, for example when someone creates an account,or comments something and etc. I found this in django documents. But I don't know where to use it. maybe in Viewset but in which method.
so what is the best way and where is the best place to send emails in Django Rest Framework?
I would like to give you couple of suggestions(Of course, with reasons):
That is synchronous. Maybe that would be not desired/efficient. I would suggest you to use something like this package (django-anymail) , this will decouple your email sender. Assume now you're using AWS SES, tomorrow you want to switch to sendgrid, then you'll only need to change setting variables with this.
Assuming User, Comment each of these are separate models. I would suggest you to override the save() method of these models and send email from there. I am not suggesting signals because I feel (fully personal view) that signals are to be used when access to the source is not possible, like some event in 3rd party library, etc.
Assuming that "Creating an account" and "commenting on something" both create some model instance, you could use existing model signals.
If you need to catch events that don't already send a signal, you will have to define new signals and send them from the appropriate place (which depends on what you're interested in so no 'one-size-fits-all' answer here).
Also if you have more than one mail to send at a time you may want to consider using some async task queue to send the emails - this avoids delaying the response (django signals are NOT async).
You can register sending function to some django signals or You can call it in Class-based Views or Function Based Views.

Add a function in models.py or managers.py

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.

Create custom django widget for timefield with now option

I have models with datetimefields and timefields. When the user interacts with these fields in a form they often just need to enter the current time. I need a now link almost exactly like what shows up in the django admin, so the user can just click it and the current time gets put in the field.
I tried looking through the django source but it seems to utilize some frontend javascript which I'm not very familiar with. Is there a simple way to make a widget that can be easily used in a timefield and datetimefield?
So this is not on the admin panel? As in on the site? Then this is not really a question to be posed to Django, I suggest tagging javascript. If you are unfamiliar with javascript, then tag jquery, they have things for this.
In case you're lazy, here's a start:
Here
jQuery premade
Javascript methods
Sorry, but this is more of a UI issue than a Django issue. Hope I helped, though.

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