django的脏场 [英] Dirty fields in django

查看:106
本文介绍了django的脏场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要保存改变的值(旧的和新的)当模型被保存。任何示例或工作代码?



我需要这个来预约内容。例如,如果用户在模型中更改某些内容,则管理员可以在单独的表中查看所有更改,然后决定是否应用它们。

解决方案

您没有对您的具体用例或需求进行非常详细的说明。特别地,知道您需要如何处理更改信息(您需要多长时间才能存储?)将会有所帮助。如果您只需要将其存储为临时目的,那么@ S.Lott的会话解决方案可能是最好的。如果您需要对存储在数据库中的对象进行所有更改的完整审计跟踪,请尝试此 AuditTrail解决方案



更新:我链接到上面的AuditTrail代码是我看到的完全解决方案最接近您的案例虽然它有一些限制(对于ManyToMany字段而言根本不起作用)。它将所有先前版本的对象存储在数据库中,所以管理员可以回滚到任何以前的版本。如果您希望更改在批准之前不会生效,那么您必须使用它。



您还可以根据类似@Armin的方式构建自定义解决方案Ronacher的DiffingMixin。你可以在表格中存储diff字典(可能是腌制的),以供管理员稍后进行审查,如果需要,请应用(您需要编写代码以使用diff字典并将其应用于实例)。


In my app i need to save changed values (old and new) when model gets saved. Any examples or working code?

I need this for premoderation of content. For example, if user changes something in model, then administrator can see all changes in separate table and then decide to apply them or not.

解决方案

You haven't said very much about your specific use case or needs. In particular, it would be helpful to know what you need to do with the change information (how long do you need to store it?). If you only need to store it for transient purposes, @S.Lott's session solution may be best. If you want a full audit trail of all changes to your objects stored in the DB, try this AuditTrail solution.

UPDATE: The AuditTrail code I linked to above is the closest I've seen to a full solution that would work for your case, though it has some limitations (doesn't work at all for ManyToMany fields). It will store all previous versions of your objects in the DB, so the admin could roll back to any previous version. You'd have to work with it a bit if you want the change to not take effect until approved.

You could also build a custom solution based on something like @Armin Ronacher's DiffingMixin. You'd store the diff dictionary (maybe pickled?) in a table for the admin to review later and apply if desired (you'd need to write the code to take the diff dictionary and apply it to an instance).

这篇关于django的脏场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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