如何获取django管理员中的对象的历史? [英] How to get the history of an object in django admin?

查看:374
本文介绍了如何获取django管理员中的对象的历史?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中一个模型中添加了一个新的字段,我想用一个完成某个操作的用户的名字(这里,由...验证):

I added a new field in one of my models and I would like to full it in with the name of a user who did a certain action (here, Validated by: USER).

我看到的最简单的方法是从历史记录中获取这个信息。

The easiest way I see is to get it from the history where this infos already is.

从这张照片,例如,我如果他执行了更改电子邮件操作,将获得用户管理员

From this picture, for example, I would get the User 'admin' if he performed the action 'Changed email'

如何获取对象的历史记录(如django Admin中所示) ?

推荐答案

该信息存储在 LogEntry 模型。

from django.contrib.admin.models import LogEntry

,其定义是:

class LogEntry(models.Model):
    action_time = models.DateTimeField(_('action time'), auto_now=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    content_type = models.ForeignKey(ContentType, blank=True, null=True)
    object_id = models.TextField(_('object id'), blank=True, null=True)
    object_repr = models.CharField(_('object repr'), max_length=200)
    action_flag = models.PositiveSmallIntegerField(_('action flag'))
    change_message = models.TextField(_('change message'), blank=True)

    objects = LogEntryManager()

源代码

确保您以 ReadOnly 模式访问此模式,否则您将打乱你的日志记录。

Make sure that you access this in ReadOnly mode, else you would be messing up your log history.

这篇关于如何获取django管理员中的对象的历史?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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