与 Django Admin 的模型历史相关联 [英] Tying in to Django Admin's Model History

查看:36
本文介绍了与 Django Admin 的模型历史相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置:

  • 我正在开发一个 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.utils.encoding import force_unicode
from django.contrib.contenttypes.models import ContentType
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
)

其中object当然是被改变的对象.

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 实现并扩展了这种方法(此处的文档).

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 Admin 的模型历史相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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