Collecting a list of fields that were updated when django saves a model record

I am creating a “What's New” section that lists all the database changes for the last day. Outside of a recent sentence, I want to use post_save or pre_save to capture fields that were changed when Django saves the model record. I will save this data in another table (with timestamp). I know that this is possible because you can observe the behavior in the administrator application - it determines which fields have been changed).

As far as I can tell, the admin application uses forms.changed_data. But using post_save or pre_save does not get form information.

Is there an effective way to determine which fields have been changed? Do I need to compare each field in the model with the current value (pre_save) to determine this list? Any help (with code examples) would be greatly appreciated.

+3
source share
1 answer

There is an answer here. Basically, you can cache your fields when initializing the object, and then in the signal post_saveyou can compare each field with a cached value ... or write a method that performs a comparison and only returns the fields that have been changed.

+1
source

Source: https://habr.com/ru/post/1771848/


All Articles