django - comparing old and new field value before saving

前端 未结 8 760
温柔的废话
温柔的废话 2020-12-12 20:40

I have a django model, and I need to compare old and new values of field BEFORE saving.

I\'ve tried the save() inheritence, and pre_save signal. It was triggered cor

8条回答
  •  余生分开走
    2020-12-12 20:57

    Also you can use FieldTracker from django-model-utils for this:

    1. Just add tracker field to your model:

      tracker = FieldTracker()
      
    2. Now in pre_save and post_save you can use:

      instance.tracker.previous('modelfield')     # get the previous value
      instance.tracker.has_changed('modelfield')  # just check if it is changed
      

提交回复
热议问题