Collecting a list of fields that have been updated when django saves a model record
I am creating a "What's New" section that lists all of the database changes in the last day. Off of a recent suggestion, I want use post_save or pre_save to capture the fields that have been altered开发者_如何学JAVA when Django saves a model record. I will save this data in another table (time-stamped). I know this is possible because one can observe the behavior in the admin application - it identifies which fields have been altered).
As best I can tell, the admin application utilizes forms.changed_data. But using post_save or pre_save does not receive any forms information.
Is there an efficient way to determine which fields have been changed? Do I have to compare each field in the model to it's current value (pre_save) to determine this list? Any help (with code examples) would be greatly appreciated.
There is an answer here. Basically you could cache your fields when initializing the object, and then in the post_save
signal you can compare every field with the cached value ... or write a method that does the comparison, and only returns the fields that have been modified.
精彩评论