django-simple-history,在管理中显示更改的字段
django-simple-history, displaying changed fields in admin
当我从 admin.ModelAdmin 继承时,在管理页面的历史记录中,我可以看到更改了哪些字段。但是,现在我需要使用 django-simple-history 来跟踪我所有的模型更改。现在,对于管理员,我继承 simple_history.SimpleHistoryAdmin。虽然我可以看到所有模型更改并还原它们,但我看不到哪些字段已更改。是否可以将这个方便的功能添加到 SimpleHistoryAdmin?
您需要的是管理员中的 history_list_display
字段。 history_list_display
中包含的字段列表及其相应的条目将显示在历史页面中。
像这样:
class SomeAdmin(admin.ModelAdmin):
def some_user_defined(self, obj):
return "something"
date_hierarchy = 'created_at'
search_fields = ['field1', 'field2']
list_display = ('field1', 'field2',)
list_filter = ('field1',)
history_list_display = ('field1', 'field2', 'some_user_defined',)
这将显示 field1
、field2
以及 comment
、user
和 reason
我找到了解决这个问题的方法。我添加了一个 ModelAdmin 方法并使用 History Diffing 在 Change history table.
中添加了一个自定义字段
history_list_display = ['changed_fields']
def changed_fields(self, obj):
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
return delta.changed_fields
return None
如果您不仅要根据 Rafi 评论查看更改字段的名称,还要查看更改的值,下一个代码将执行此操作:
def changed_fields_with_values(self, obj):
fields = ""
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
for change in delta.changes:
fields += str("{} changed from {} to {}".format(change.field, change.old, change.new))
return fields
return None
类似于 Rafi 之前的解决方案,但使用数组更优雅地列出记录更改:
def list_changes(self, obj):
diff = []
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
for change in delta.changes:
diff.append("<b>* {}:</b> changed from `{}` to `{}`".format(change.field, change.old, change.new))
return mark_safe("\n<br>".join(diff))
你可能想做这样的事情:
# admin.py
from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Website
from django.utils.html import format_html
class WebsiteHistoryAdmin(SimpleHistoryAdmin):
history_list_display = ["changed_fields","list_changes"]
def changed_fields(self, obj):
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
return delta.changed_fields
return None
def list_changes(self, obj):
fields = ""
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
for change in delta.changes:
fields += str("<strong>{}</strong> changed from <span style='background-color:#ffb5ad'>{}</span> to <span style='background-color:#b3f7ab'>{}</span> . <br/>".format(change.field, change.old, change.new))
return format_html(fields)
return None
admin.site.register(Website, WebsiteHistoryAdmin)
结果是:
当我从 admin.ModelAdmin 继承时,在管理页面的历史记录中,我可以看到更改了哪些字段。但是,现在我需要使用 django-simple-history 来跟踪我所有的模型更改。现在,对于管理员,我继承 simple_history.SimpleHistoryAdmin。虽然我可以看到所有模型更改并还原它们,但我看不到哪些字段已更改。是否可以将这个方便的功能添加到 SimpleHistoryAdmin?
您需要的是管理员中的 history_list_display
字段。 history_list_display
中包含的字段列表及其相应的条目将显示在历史页面中。
像这样:
class SomeAdmin(admin.ModelAdmin):
def some_user_defined(self, obj):
return "something"
date_hierarchy = 'created_at'
search_fields = ['field1', 'field2']
list_display = ('field1', 'field2',)
list_filter = ('field1',)
history_list_display = ('field1', 'field2', 'some_user_defined',)
这将显示 field1
、field2
以及 comment
、user
和 reason
我找到了解决这个问题的方法。我添加了一个 ModelAdmin 方法并使用 History Diffing 在 Change history table.
中添加了一个自定义字段history_list_display = ['changed_fields']
def changed_fields(self, obj):
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
return delta.changed_fields
return None
如果您不仅要根据 Rafi 评论查看更改字段的名称,还要查看更改的值,下一个代码将执行此操作:
def changed_fields_with_values(self, obj):
fields = ""
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
for change in delta.changes:
fields += str("{} changed from {} to {}".format(change.field, change.old, change.new))
return fields
return None
类似于 Rafi 之前的解决方案,但使用数组更优雅地列出记录更改:
def list_changes(self, obj):
diff = []
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
for change in delta.changes:
diff.append("<b>* {}:</b> changed from `{}` to `{}`".format(change.field, change.old, change.new))
return mark_safe("\n<br>".join(diff))
你可能想做这样的事情:
# admin.py
from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Website
from django.utils.html import format_html
class WebsiteHistoryAdmin(SimpleHistoryAdmin):
history_list_display = ["changed_fields","list_changes"]
def changed_fields(self, obj):
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
return delta.changed_fields
return None
def list_changes(self, obj):
fields = ""
if obj.prev_record:
delta = obj.diff_against(obj.prev_record)
for change in delta.changes:
fields += str("<strong>{}</strong> changed from <span style='background-color:#ffb5ad'>{}</span> to <span style='background-color:#b3f7ab'>{}</span> . <br/>".format(change.field, change.old, change.new))
return format_html(fields)
return None
admin.site.register(Website, WebsiteHistoryAdmin)
结果是: