问题
How to make history for a model that show me the old value and the new changed value in a field?
In my mind i think to make history model to each model which have the same fields and when the user create new or update the data from the first model go to the second model too.
I searched alot for this problem and i found a packet called django-reversion but i didn't understand it how to make it store the old value and new value if any one can help me for this problem please tell me.
回答1:
For this purpose you can use simple-history app
Every changes of your instance will be reflected in historical table. There would be new row with new values for each changes. so you can reproduce all old values for each field.
回答2:
There may be a clean library that does everything you need, I just don't know.
How I'd approach this problem is overriding the model save function. You can refer to this prior stackoverflow post to get the gist of what is going on. In this way you could specify which model class/ sql table you'd like to apply memory for. And then add to the custom save method to create a new row in the new change log model class.
回答3:
In this link there is a comparisson of different packages that can be used for audit/history of models.
I use django-simple-history which comes with a nice integration to view the list of changes of every model instance in the admin.
For my use case at least, django-simple-history provided me with what I needed, and so far I did not face any issues with it.
EDIT: just an additional note
This package does NOT track history of ManyToManyField
.
See also these related questions:
- How can I store history of ManyToManyField using django-simple-history.
- History of a ManyToManyField in django-simple-history
And the Github issue:
- https://github.com/treyhunner/django-simple-history/issues/16
来源:https://stackoverflow.com/questions/50029714/what-its-the-best-way-to-make-full-history-for-a-model-in-django