0

我正在使用 django-simple-history==1.9.0 包和 django 1.8。当我在管理员之外创建一个对象,然后在管理页面中查看该对象的历史记录时,它会显示一条消息

此对象没有更改历史记录。它可能不是通过此管理站点添加的。

我尝试为该对象设置用户:

user = User.objects.get(username='john')
Poll.objects.get(id=536).history.update(history_user=user)

但这并没有解决问题。

Poll.objects.get(id=536).history.all().count()

返回 1,因此生成了历史记录。任何想法如何让它显示历史或如何创建额外的历史?我也试过update_change_reason但这根本不起作用。

4

2 回答 2

4

假设您的 django-simple 配置正确,请按照以下步骤操作

model.py要更改 import django-simple-history的应用程序文件中,导入以下摘录:

from simple_history.models import HistoricalRecords

model.py文件中,添加history属性如下:

history = HistoricalRecords()

例子:

from django.db import models
from simple_history.models import HistoricalRecords

class Poll(models.Model):
    question = models.CharField(max_length=200)
    history = HistoricalRecords()

为了让您在 admin 之外所做的更改出现在 Django admin 中,只需在admin.py文件中添加以下代码:

进口:

from simple_history.admin import SimpleHistoryAdmin

使用寄存器配置管理员历史记录:

admin.site.register(Pool, SimpleHistoryAdmin)

例子:

from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Pool

# Register your models here.
admin.site.register(Tag, SimpleHistoryAdmin)

在此之后,您的历史记录将出现在管理员中。

资料来源:

https://django-simple-history.readthedocs.io/en/latest/admin.html

https://django-simple-history.readthedocs.io/en/latest/user_tracking.html

问候,

费利佩·多明格斯

Web开发人员

于 2018-01-12T01:57:05.867 回答
0

显然我需要在 LogEntry 中创建日志,如下例所示,因为 django-simple-history 不会跟踪管理页面之外的更改:

from django.contrib.admin.models import LogEntry
from django.contrib.admin.models import LogEntryManager, ADDITION, CHANGE
user_id = User.objects.all()[0].id
content_type_id = ContentType.objects.get(model='color').id
object_id = 4
object_repr = 'Color object'
action_flag = CHANGE
change_message = 'you changed it!'
LogEntry.objects.log_action(user_id, content_type_id, object_id, object_repr, action_flag, change_message=change_message)
于 2017-12-19T08:15:08.000 回答