我已经使用 django-simple-history 实现了对 Django 中对象更改的历史跟踪 https://django-simple-history.readthedocs.io/en/2.10.0/index.html
我在模型中提到了一个字段history
,我可以使用它查询对象的历史记录。
history = HistoricalRecords()
我们还在数据库中创建了一个名为appname_historicalmodelname
的表。
我想在 django 管理中appname_historicalmodelname
显示此表,其中我们有按history_time
排序的记录列表。
由于我没有该历史记录表的模型类,因此无法使用admin.site.register(HistoricalModelName)
。如何在 Django 管理站点中显示此表?
- 姜戈:1.11
- 蟒蛇:2.7
django-simple-history
附带SimpleHistoryAdmin
,它固定在普通管理视图的"历史记录"按钮上,允许您查看特定模型实例的历史记录。但是,如果您希望能够为历史记录本身创建 django 管理视图,您可以执行以下操作(假设在应用程序my_app
中CustomModel
基本模型(:
from django.apps import apps
from django.contrib import admin
HistoricalCustomModel = apps.get_model("my_app", "HistoricalCustomModel")
@admin.register(HistoricalCustomModel)
class HistoricalCustomModelAdmin(admin.ModelAdmin):
...