编程语言
首页 > 编程语言> > python-跟踪Django中所有模型的更改

python-跟踪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-reversiondjango-simple-historydjango-audit-logdjango-historicalrecords,但是我不明白我应该如何使用这些软件包以及为什么要使用这些软件包,因为其中某些软件包似乎无法满足要求.因此,经过两天的搜索和阅读有关如何跟踪模型更改的大量帖子,我基本上什么也没做.

我是Django新手,不胜感激.

如果不清楚,请随时评论您的查询.提前致谢 :)

解决方法:

您是否曾浏览过pre_save的django信号?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.

标签:database-versioning,python,django,django-models
来源: https://codeday.me/bug/20191026/1934788.html