I am working on a Django + DRF Web Application and I want to track changes to all model instances in the Database and keep a log of all changes made, namely:
TABLE - Table to which record was added/modified
FIELD - Field that was modified.
PK_RECORD - Primary Key of the model instance that was modified.
OLD_VAL - Old Value of the field.
NEW_VAL - New Value of the field.
CHANGED_ON - Date it was changed on.
CHANGED_BY - Who changed it?
REVISION_ID - Revision ID of the current Model Instance.
Later on, I want the user to be able to track the changes made to the model and see which version of the instance was used for a particular action so everything can be tracked.
For this purpose, I tried to understand the various packages in django for tracking database model changes, some of them listed here:
django-model-audit packages
I tried django-reversion, django-simple-history, django-audit-log, django-historicalrecords, but I fail to understand how and why I should use each of these packages as some of them seem like an overkill for the requirements. So, after two days of searching and reading through numerous posts about how I should be tracking model changes, I have basically done nothing.
I am new to Django and would appreciate any help.
If something is not clear, feel free to comment your queries. Thanks in advance :)
Have you explored on django signals pre_save?https://docs.djangoproject.com/en/dev/topics/signals/
from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import MyModel
@receiver(pre_save, sender=MyModel)
def my_handler(sender, instance=None, **kwargs):
# instance variable will have the record which is about to be saved.
# So log your details accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With