Can't get post_save to work in Django

I read the django docs near signals and wrote this code snippet for my Car model:

 @receiver(request_finished) def signal_callback(sender, **kwargs): print 'Save Signal received' @receiver(post_save, sender=Car) def signal_handler(sender, **kwargs): pass request_finished(signal_callback, sender=car, dispatch_url="Unique save id") 

But the problem is that when I start my server and just open the administrator, I get a lot of 'Save Signal received' in my terminal. I am wondering, I limited only signal_handler to post_save . But still, without even saving anything, the message appears many times. I do not understand this.

Note: I will be honest. I understood parts of it, not all of the documentation.

+1
source share
2 answers

There is an easier way to bind post_save signals

 from django.db.models.signals import post_save from myapp.models import Car def do_something(sender, **kwargs): print 'the object is now saved.' car = kwargs['instance'] #now i have access to the object post_save.connect(do_something, sender=Car) 

The request finished signal receives a call each time an HTTP request is executed, which is a hang.

+5
source

You have bound the request_finished signal to signal_callback . Remove (or write down) the signal_callback and change the signal_handler as follows.

 @receiver(post_save, sender=Car) def signal_handler(sender, **kwargs): print 'Save signal received' 
+1
source

Source: https://habr.com/ru/post/1487757/


All Articles