Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what its the best way to make full history for a model in django?

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.

like image 519
Ahmed Othman Mohamed Nazim Avatar asked Apr 25 '18 19:04

Ahmed Othman Mohamed Nazim


People also ask

How use Django simple history?

To allow viewing previous model versions on the Django admin site, inherit from the simple_history. admin. SimpleHistoryAdmin class when registering your model with the admin site. This will replace the history object page on the admin site and allow viewing and reverting to previous model versions.

What to do after creating models in Django?

Once you have defined your models, you need to tell Django you're going to use those models. Do this by editing your settings file and changing the INSTALLED_APPS setting to add the name of the module that contains your models.py .

What is timestamped model in Django?

TimeStampedModel - An Abstract Base Class model that provides self-managed created and modified fields.

Is there a list field in Django models?

Each field has a set of arguments for setting column properties. For example, to define the varchar database, CharField requires max_length. These are called Field options in Django model data types and field lists.


2 Answers

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.

like image 151
Daniil Mashkin Avatar answered Sep 28 '22 05:09

Daniil Mashkin


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:

  1. How can I store history of ManyToManyField using django-simple-history.
  2. History of a ManyToManyField in django-simple-history

And the Github issue:

  1. https://github.com/treyhunner/django-simple-history/issues/16
like image 43
Ralf Avatar answered Sep 28 '22 06:09

Ralf