Django Model History
The package is designed to track changes in object models. It has the ability to roll back changes to the object and attempt to restore an object if it has been deleted. If the change of the object were conducted script or from the console, the user field will be empty.
Quick start
-
Install:
pip install django-model-history2
-
Add in INSTALLED_APPS:
INSTALLED_APPS = ( ... 'model_history' )
-
Make migrate:
python manage.py migrate model_history
-
Add decorator model_hisotry to your Model class:
from model_history.decorators import model_history
@model_history()
class SomeModel(models.Model):
field_one = models.CharField()
last_modified = models.DateTimeField()
Arguments for decorator model_history
-
exclude
- Exclude fields. Default: []:@model_hisotry(exclude=['last_modified'])
-
related
- Track all related models. Default: False:@model_history(exclude=['last_modified'], related=True)
-
related_exclude
- Exclude for related models. Default: []:@model_history(exclude=['last_modified'], related=True, related_exclude=['myapp.models.Model2'])
Settings
-
The settings are defined in the dictionary named
MODEL_HISTORY_SETTINGS
:MODEL_HISTORY_SETTINGS = { 'connect': [ {'model': 'django.contrib.auth.models.User', 'exclude': ['last_login'], 'related': True, 'related_exclude': ['django.contrib.admin.models.LogEntry']} ] }
-
Models are listed in parameter
connect
will be put on track, with the specified parameters. -
delete_action
- Should be in the admin interface function "delete selected objects". Default: False:MODEL_HISTORY_SETTINGS = { 'delete_action': True 'connect': [ {'model': 'django.contrib.auth.models.User', 'exclude': ['last_login'], 'related': True, 'related_exclude': ['django.contrib.admin.models.LogEntry']} ] }
-
delete_permission
- Which option is the user to check for compliance with the availability of the right to delete entries from the history:MODEL_HISTORY_SETTINGS = { 'delete_action': True, 'delete_permission': 'is_superuser', 'connect': [ {'model': 'django.contrib.auth.models.User', 'exclude': ['last_login'], 'related': True, 'related_exclude': ['django.contrib.admin.models.LogEntry']} ] }
-
revert_action
- Should be in the admin function "Revert". This function tries to make changes or cancellation recover a deleted object from the dump. Default: True:MODEL_HISTORY_SETTINGS = { 'delete_action': True, 'delete_permission': 'is_superuser', 'revert_action': True, 'connect': [ {'model': 'django.contrib.auth.models.User', 'exclude': ['last_login'], 'related': True, 'related_exclude': ['django.contrib.admin.models.LogEntry']} ] }
-
revert_permission
- Which option is the user to check for compliance with the availability of rights to recovery facility:MODEL_HISTORY_SETTINGS = { 'delete_action': True, 'delete_permission': 'is_superuser', 'revert_action': True, 'revert_permission': 'is_superuser', 'connect': [ {'model': 'django.contrib.auth.models.User', 'exclude': ['last_login'], 'related': True, 'related_exclude': ['django.contrib.admin.models.LogEntry']} ] }
Changelist
- 0.9.3:
- Add
track_created
inMODEL_HISTORY_SETTINGS
for tracking new object created. Default: False.- Add
dispatch_uid
for connected signals.- Model
History
small refactoring.
- 0.9.2:
- Refactor settings import