Detect if a model has changed before calling save in Django

后端 未结 5 2095
Happy的楠姐
Happy的楠姐 2020-12-08 21:19

I have a database model that is being updated based on changes in remote data (via an HTML scraper).

I want to maintain a field called changed - a times

相关标签:
5条回答
  • 2020-12-08 21:35

    Sounds to me like what you want is Signals: http://docs.djangoproject.com/en/1.2/topics/signals/

    You could use a post_save signal to update a related field in another model to store the previous value. Then on the next go-round you'd have something to compare.

    0 讨论(0)
  • 2020-12-08 21:39

    http://code.activestate.com/pypm/django-dirtyfields/

    Tracks dirty/changed fields on a django model instance.

    0 讨论(0)
  • 2020-12-08 21:41

    If you save your instance through a form, you can check form.has_changed().

    0 讨论(0)
  • 2020-12-08 21:41

    You might try computing a checksum of the record values when you save them. Then when you read it later, recompute the checksum and see if it has changed. Perhaps the crc32 function in the Python zlib standard module. (I'm not sure what kind of performance this would have. So you may want to investigate that.)

    0 讨论(0)
  • 2020-12-08 21:51

    This library has tracks FK lookups.

    https://github.com/mmilkin/django_dirty_bits

    0 讨论(0)
提交回复
热议问题