在Django的simple_history中使用diff_against指定"excluded_fields"的方法如下:
from django.contrib.admin.models import LogEntry
from simple_history.admin import SimpleHistoryAdmin
class CustomHistoryAdmin(SimpleHistoryAdmin):
def diff_against(self, old, new, exclude=None):
# 排除特定字段
excluded_fields = ['field1', 'field2']
if exclude:
excluded_fields.extend(exclude)
# 使用SimpleHistoryAdmin的diff_against方法
return super().diff_against(old, new, exclude=excluded_fields)
在这个自定义的Admin类中,我们重写了diff_against方法,并添加了一个名为"excluded_fields"的列表,其中包含了要排除的字段。通过将这个列表传递给super().diff_against方法,我们可以实现指定排除字段的效果。
@admin.register(YourModel)
class YourModelAdmin(CustomHistoryAdmin):
pass
通过将YourModelAdmin类注册到@admin.register装饰器中,并将CustomHistoryAdmin类作为base_admin属性的值,你就可以在Django的simple_history中使用diff_against时指定"excluded_fields"了。
请注意,这里只是一个示例,你可以根据自己的需求进行相应的修改和调整。对于更多关于Django的simple_history模块的详细信息,可以参考腾讯云的Django simple_history文档。
领取专属 10元无门槛券
手把手带您无忧上云