2017-02-24 142 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

謝謝。將看到並讓你知道。 :) –