django-simple-history,在admin中显示更改的字段

wbrvyc0a  于 2023-06-25  发布在  Go
关注(0)|答案(6)|浏览(154)

当我从admin.ModelAdmin继承时,在管理页面的历史记录中,我可以看到哪些字段被更改了。但是,现在我需要使用django-simple-history来跟踪我所有的模型更改。现在,对于admin,我继承了simple_history. SimpleHistoryAdmin。虽然我可以看到所有的模型更改并还原它们,但我无法看到哪些字段被更改。是否有可能将该方便的功能添加到SimpleHistoryAdmin中?

bbuxkriu

bbuxkriu1#

我找到了解决这个问题的方法。我添加了一个ModelAdmin方法,并使用HistoryDiffing在Changehistory表中添加了一个自定义字段。

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
os8fio9y

os8fio9y2#

您需要的是管理员中的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',)

这将显示field1field2沿着commentuserreason

brtdzjyr

brtdzjyr3#

你可能想做这样的事情:

# 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)

你会得到这样的结果:

wgxvkvu9

wgxvkvu94#

如果你不仅想查看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
zvms9eto

zvms9eto5#

类似于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))
wswtfjt7

wswtfjt76#

根据@Rafi的建议,我为此创建了一个类,可以用作mixin:

class EnhancedSimpleHistoryAdmin(SimpleHistoryAdmin):

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        if hasattr(cls, 'history_list_display'):
            cls.history_list_display.append('changed_fields')
        else:
            cls.history_list_display = ['changed_fields']

    def changed_fields(self, obj):
        if obj.prev_record:
            delta = obj.diff_against(obj.prev_record)
            return ", ".join(delta.changed_fields)
        return None

相关问题