python - 跟踪 Django 中所有模型的更改

标签 python django django-models database-versioning

我正在开发一个 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.

关于python - 跟踪 Django 中所有模型的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42433501/

相关文章:

javascript - 从文本中删除内联样式

python - 具有自定义模板的 Django formtools

python - django rest 框架嵌套模型序列化器

django 1.5 扩展默认用户模型或替换它

python - Django 从模型 : Model. objects.filter(Q()) 中过滤

python 将字符串每隔 3 个值拆分一次,但拆分为嵌套格式

python - Django 管理员并显示缩略图

python - 具有不同类型问题的 Django 测验应用程序

django - 是否可以阻止 Django 在原始 SQL 参数周围添加引号?

python - 在python中将int转换为特定大小的字节