Clément
Clément

Reputation: 98

How to use the data about objects modifications in Django?

Django stores a history of the modification for every object, it is something we can access to through the Django admin:

History button

It contains data about when the object was created/modified, the user who performed the action and the timestamp of the action:

enter image description here

By giving a look at the database, I can guess this data is stored in a default table called django_admin_log:

enter image description here

I am wondering if we can make use of this data in any way through the instance of a model ? I got used to adding manually my timestamps on every models through an Abstract Base Class, but I am wondering if it is useful in any way ?

Or this table records only the modification taking place in the Django admin panel, which would makes the custom timestamp still needed for when the models instance were to be updated outside it.

Upvotes: 0

Views: 287

Answers (1)

hendrikschneider
hendrikschneider

Reputation: 1846

The history is only related to actions done in the admin view. To add metadata you can also use model_utils, which also offers some other handy functionalities: https://django-model-utils.readthedocs.io/en/latest/

Let us assume every action would be stored in a history table. This would indicate that you always have make a join in the db to get a view where each row also has created and updated information. This is quite some overhead. Therefore, keep it simple and add the timestamp to each model :)

Upvotes: 1

Related Questions