django post_save signals on update

后端 未结 2 1551
孤独总比滥情好
孤独总比滥情好 2020-12-28 14:16

I am trying to set up some post_save receivers similar to the following

@receiver(post_save, sender=Game, dispatch_uid=\'game_updated\')
def game_updated(sen         


        
2条回答
  •  时光说笑
    2020-12-28 14:57

    Just one more thing to @Ismali Badawi's answer.


    This calls post_save

    user = User.objects.get(id=1) 
    user.username='edited_username' 
    user.save()
    

    This does not call post_save

    User.objects.filter(id=1).update(username='edited_username')
    

    In the code,

    from django.db.models.signals import post_save
    
    @receiver(post_save, sender=User)
    def do_something_when_user_updated(sender, instance, created, **kwargs):
        if not created:
            # User object updated
            user_obj = instance
            pass
    

提交回复
热议问题