Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django simple-history in admin

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?

like image 868
user773328 Avatar asked Oct 19 '11 15:10

user773328


1 Answers

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)
like image 83
dnozay Avatar answered Sep 27 '22 02:09

dnozay