2017-02-24 89 views
1

我正在一个Django + DRF Web应用程序工作的所有车型,我想跟踪更改数据库的所有模型实例,并保留日志的制作,即所有的变化:跟踪变化在Django

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的新手,希望有任何帮助。

如果有什么不清楚的地方,请随时评论您的查询。在此先感谢:)

回答

0

你探讨了django信号pre_save? https://docs.djangoproject.com/en/1.10/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. 
+0

谢谢。将看到并让你知道。 :) –