跟踪 Django 中所有模型的更改

Tracking changes to all models in Django

我正在开发一个 Django + DRF Web 应用程序,我想跟踪对数据库中所有模型实例的更改并记录所做的所有更改,即:

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.

稍后,我希望用户能够跟踪对模型所做的更改,并查看哪个版本的实例用于特定操作,以便跟踪所有内容。

为此,我试图了解 django 中用于跟踪数据库模型更改的各种包,其中一些包列在这里:

django-model-audit packages

我试过 django-reversion, django-simple-history, django-audit-log, django-historicalrecords,但我不明白我应该如何以及为什么要使用这些包中的每一个,因为其中一些包似乎对需求来说有点矫枉过正。所以,在两天的搜索和阅读大量关于我应该如何跟踪模型变化的帖子之后,我基本上什么也没做。

我是 Django 的新手,非常感谢任何帮助。

如果有什么不清楚的地方,请随时评论您的问题。提前致谢:)

您是否探索过 django 信号 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.