首页 > 解决方案 > 如何在 Django 管理站点中显示 django-simple-history 的历史表?

问题描述

我已经使用 django-simple-history https://django-simple-history.readthedocs.io/en/2.10.0/index.html实现了对 Django 中对象更改的历史跟踪

我已经提到了historyModel 中的一个字段,我可以使用它来查询对象的历史记录。

history = HistoricalRecords()

我们还在数据库中创建了一个名为 name 的表appname_historicalmodelname

我想appname_historicalmodelname在 django admin 中显示此表,其中我们有按 . 排序的记录列表history_time

由于我没有该 History 表的 Model 类,因此我无法使用admin.site.register(HistoricalModelName). 如何在 Django 管理站点中显示此表?

标签: pythondjangodjango-modelsdjango-admindjango-simple-history

解决方案


django-simple-history附带SimpleHistoryAdmin,将“历史”按钮固定在您的正常管理视图中,允许您查看特定模型实例的历史记录。但是,如果您希望能够为历史记录本身创建 django 管理视图,您可以执行以下操作(假设CustomModelapp 中的基本模型my_app):

from django.apps import apps
from django.contrib import admin

HistoricalCustomModel = apps.get_model("my_app", "HistoricalCustomModel")

@admin.register(HistoricalCustomModel)
class HistoricalCustomModelAdmin(admin.ModelAdmin):
    ...

推荐阅读