我已经创建了一个简单的 Django 应用程序来显示单个文章。这些文章有许多用户可以编辑的字段。我正在使用包 'django-auditlog' 来记录对这些文章模型的更改。到目前为止,我只需遵循auditlog installation doc来设置模型历史记录跟踪 (以及使中间件允许 'actor_id' 被跟踪)。我还添加了示例代码,如最近显示单个模型
<!-- History display -->
<div cl="table-responsive">
<table id="history" cl="table table-striped table-bordered">
<thead>
<tr>
<th>Actor</th>
<th>Field</th>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody>
<!-- Human readable - change to '.changes_dict.' for proper logs -->
{% for key, value in article.history.latest.changes_display_dict.items %}
<tr>
<td>{{ article.history.latest.author_id }}</td>
<td>{{ key }}</td>
<td>{{ value.0|default:"None"|striptags|safe }}</td>
<td>{{ value.1|default:"None"|striptags|safe }}</td>
</tr>
{% empty %}
<p>No history for this item has been logged yet.</p>
{% endfor %}
</tbody>
</table>
</div>
正如我的代码可能建议的那样,我试图向历史记录表添加一个额外的列,以显示谁进行了正在显示的更改。
是否有一个简单的方法通过 auditlog 做到这一点,或者我必须创建某种 sql 查询到我的 sqlite auditlog db 表来检索 'author_id' 字段?
非常感谢。
在查看 Django AuditLog 的models file后,我找到了答案。如果您使用 django-auditlog 教程中描述的 AuditlogHistoryField()方法创建了历史记录字段,则无法直接从模型的历史记录字段中拉出 actor。
相反,我做了以下操作:
在 views.py 文件中
from auditlog.models import LogEntry
...
dal_log = LogEntry.objects.filter(object_id=article.id)
...
context = {'article': article, 'logs': dal_log}
return render(request, "detail.html", context)
然后在我的模板中,我能够使用指定对象的日志条目(在我的情况下,这些是 'article' 模型)。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(2条)