开发者

Custom View on a table in the administration console

I have a simple table in my Django app that looks like:

class Setting(models.Model):
    group = models.CharField(null=False, max_length=255)
    name = models.CharField(null=False, max_length=255)
    value= models.CharField(null=False, max_length=255)

For reasons, I'm storing the data as key value pairs. I'm the administration console, I'd like to represent this Model pivoted i.e. transpose the rows to become columns. I'd like to it behave as any other model admin.

For example when record is saved, I'll be iterating over the fields and saving them which I'll do a over-ridden manager method. When the admin tries to list all the rows from this view, Id would iterating over the key-value pairs and pivot开发者_如何转开发ing them.

Any ideas on how to accomplish this?

Thanks


Simple solution - make the fields in admin's object list editable:

class SettingsAdmin(admin.ModelAdmin):
    list_display = ('group', 'name', 'value')
    list_editable = ('value',)    # Fields that will be editable
    list_display_links = ()

If that's not satisfactory, you can use custom template file for the change list view. You need to prepare a template file that will render your data and set the path in ModelAdmin:

class SettingsAdmin(admin.ModelAdmin):
    change_list_template = 'setting_change_list.html'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜