管理中的 Django 简单历史记录

发布于 2024-12-10 22:38:17 字数 245 浏览 0 评论 0原文

我想向 django simple-history 添加管理视图功能。我在模型上创建了历史属性,该模型现在自动显示在管理文档部分中,无需我提供任何进一步的代码,但它不会出现在管理部分中。我希望用户能够查看更改的历史记录并使用most_recent 函数应用撤消函数。 您对如何解决这个问题有什么建议吗?

I would like to add admin view capability to django simple-history. I created a history attribute on a model and this model now appears in the admin docs section automatically without any further code from me, but it does not appear in the admin section. I want users to be able to see the history of changes and to apply an undo function using the most_recent function.
Do you have any suggestions for how to approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是一片海 2024-12-17 22:38:17

如果您的模型是:

from simple_history.models import HistoricalRecords
from django.db import  models

class Poll(models.Model):
    question = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published')
    history = HistoricalRecords()

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    history = HistoricalRecords()

那么您可以拥有一个如下所示的管理员:

from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Poll, Choice

admin.site.register(Poll, SimpleHistoryAdmin)
admin.site.register(Choice, SimpleHistoryAdmin)

或者您可以自定义它...

from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Poll

class PollAdmin(SimpleHistoryAdmin):
    list_display = ('question', 'pub_date')

admin.site.register(Poll, PollAdmin)

if your models are:

from simple_history.models import HistoricalRecords
from django.db import  models

class Poll(models.Model):
    question = models.CharField(max_length = 200)
    pub_date = models.DateTimeField('date published')
    history = HistoricalRecords()

class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.CharField(max_length=200)
    votes = models.IntegerField()
    history = HistoricalRecords()

then you can have an admin that looks like:

from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Poll, Choice

admin.site.register(Poll, SimpleHistoryAdmin)
admin.site.register(Choice, SimpleHistoryAdmin)

or you can customize it ...

from django.contrib import admin
from simple_history.admin import SimpleHistoryAdmin
from .models import Poll

class PollAdmin(SimpleHistoryAdmin):
    list_display = ('question', 'pub_date')

admin.site.register(Poll, PollAdmin)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文