How to determine if a field has changed in a Django modelform

后端 未结 4 1359
甜味超标
甜味超标 2021-01-04 07:25

I was surprised that this was difficult to do. However I came up with this, which seems to work at least for my simple case. Can anyone recommend a better approach?

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-04 08:02

    You must override the method post_save. Overriding methods is a good practice in Django: https://docs.djangoproject.com/en/dev/ref/signals/#post-save

    I suggest you adding something like this into your model class:

        def post_save(self, sender, instance, created, raw, using, update_fields):
            if 'the_field' in update_fields:
               # Do whatever...
    

提交回复
热议问题