5

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?

4

1 回答 1

11

如果您的模型是:

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)
于 2013-04-23T15:43:19.263 回答