Identify the changed fields in django post_save signal

前端 未结 7 1449
再見小時候
再見小時候 2020-12-02 15:28

I\'m using django\'s post_save signal to execute some statements after saving the model.

class Mode(models.Model):
    name = models.CharField(max_length=5)
         


        
7条回答
  •  再見小時候
    2020-12-02 15:58

    You can use update_fields in django signals.

    @receiver(post_save, sender=Mode)
    def post_save(sender, instance, created, **kwargs):
    
        # only update instance
        if not created:
    
            update_fields = kwargs.get('update_fields') or set()
    
            # value of `mode` has changed:
            if 'mode' in update_fields:
                # then do this
                pass
            else:
                # do that
                pass
    

提交回复
热议问题