绑在Django管理员的模特历史 [英] Tying in to Django Admin's Model History

查看:112
本文介绍了绑在Django管理员的模特历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:


  • 我正在使用一个Django应用程序,允许用户创建一个对象在数据库中,然后返回并尽可能多地编辑它。

  • Django的管理员网站保留了通过管理网站对对象所做的更改的历史。

问题:


  • 我将应用程序挂接到管理站点的更改历史记录,以便我可以看到用户对其内容所做的更改的历史记录?

推荐答案

管理历史只是一个像任何其他Django应用程序一样的应用程序,除了在管理站点上是特殊放置。

The admin history is just an app like any other Django app, with the exception being special placement on the admin site.

模型在django.contrib.admin.models.LogEntry。

The model is in django.contrib.admin.models.LogEntry.

当用户进行更改时,添加到这样的日志(从contrib / admin无耻地偷走/options.py:

When a user makes a change, add to the log like this (stolen shamelessly from contrib/admin/options.py:

from django.contrib.admin.models import LogEntry, ADDITION
LogEntry.objects.log_action(
    user_id         = request.user.pk, 
    content_type_id = ContentType.objects.get_for_model(object).pk,
    object_id       = object.pk,
    object_repr     = force_unicode(object), 
    action_flag     = ADDITION
)

其中对象是当然更改的对象。

where object is the object that was changed of course.

现在我看到丹尼尔的答案,并同意他的看法,这是非常有限的。

Now I see Daniel's answer and agree with him, it is pretty limited.

在我看来,一个更强大的方法是使用Marty Alchin的代码在他的书 Pro Django (请参阅第263页的保存历史记录)。有一个应用程序 django-simple-history ,它实现和扩展了这种方法( docs here )。

In my opinion a stronger approach is to use the code from Marty Alchin in his book Pro Django (see Keeping Historical Records starting at page 263). There is an application django-simple-history which implements and extends this approach (docs here).

这篇关于绑在Django管理员的模特历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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