Cant get post_save to work in Django

前端 未结 2 1860
北海茫月
北海茫月 2021-01-15 07:51

I read the django docs about signals and wrote this piece of code for my model Car :

@receiver(request_finished)
def s         


        
相关标签:
2条回答
  • 2021-01-15 08:06

    There is a simpler 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 signal request finished gets called every time a HTTP request is made, which is a hog.

    0 讨论(0)
  • 2021-01-15 08:29

    You binded request_finished signal to signal_callback. Remove(or comment out) signal_callback, and change signal_handler as follow.

    @receiver(post_save, sender=Car)
    def signal_handler(sender, **kwargs):
        print 'Save signal received'
    
    0 讨论(0)
提交回复
热议问题