在django中创建“历史”类型模型的最好方法是什么? [英] What's the best way to create a 'history' type model in django?

查看:149
本文介绍了在django中创建“历史”类型模型的最好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的Django应用程式建立一个类似于Django管理员的「最近操作」的功能,以便将纪录资讯储存在其他型号上。

I'd like to create a feature for my Django app similar to Django admin's 'Recent Actions', in order to store history information on my other models.

例如说我有两个模型叫做书和作者。我想有一个第三个模型,存储信息,例如对模型中的给定对象执行什么操作(添加,修改,删除等),由谁和何时执行。

For example say I have two models called Book and Author. I want to have a third model that stores information such as what action was performed on a given object in a model (add, modify, delete, etc.) by who and when.

谁,当什么时候和行动都很容易,我只是不确定如何存储关于执行操作的对象的信息。

Who, when and the action are easy, I'm just unsure about how to store information regarding what object the action was performed on.

我的初始想法是有一个事务模型,将存储此信息,我的书和作者模型可以有一个ForeignKey的关系。但是,如果我删除给定的图书或作者,那么它的交易历史也会被删除,并且我没有记录表明此对象确实已删除。

My initial idea was to have a 'Transactions' model that would store this information, and both my Book and Author models could have a ForeignKey relation to it. However, if I delete the given book or author, then its transaction history is also deleted and I have no record that this object was indeed deleted.

的其他可能的解决方案,但我想我会在这里要求更多经验的意见。我应该如何处理这个问题和什么是一些合理的解决方案?

I've been thinking of other possible solutions, but I thought I'd ask for more experienced opinions here first. How should I approach this problem and what are some reasonable solutions to it?

谢谢!

推荐答案

您可能想查看 django-reversion - - 要么实现或从中学习为自己的实现。 Django还原工作通过保存关于更改的信息,以及项目的序列化副本,因为它在那一刻存在。

You might want to check out django-reversion -- Either to implement or to learn from it for your own implementation. Django reversion works by saving information about the change, along with a serialized copy of the item as it existed at that one moment. This means that you can later recall that one specific item and do a comparison or restoration or whatever.

如果您决定实现自己的自定义解决方案,我建议您使用自定义解决方案使用Django内置的信号子系统。 Django开箱即用提供了你提到的事情的常见信号,如模型创建,保存和删除。

If you decide to implement your own custom solution, I'd recommend making use of Django's built in signal sub-system. Django out-of-the-box provides the common signals for things you mentioned like model creation, saving, and deleting. You could add in custom signals as well if you found the need for them.

使用信号将允许您开发独立于书/作者实现的历史代码代码,从长远来看将是一个资产(相对于整个书/作者代码中的字符串历史代码)。再次django-reversion作为一个很好的参考,通过做它的工作几乎完全与信号。

Using signals would allow you to develop the 'history' code separate from the Book/Author implementation code which in the long run will be an asset (as opposed to stringing history code throughout the Book/Author code). Again django-reversion serves as a good reference here by doing its work almost entirely with signals.

这篇关于在django中创建“历史”类型模型的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆