How to add 'collapse' to a Django StackedInline

In the same way you can add 'classes': ['collapse'] to one of your ModelAdmin fieldsets, I'd like to be able to have an Inline Model Admin be collapsible.

This ticket, Collapse in admin interface for inline related objects, discusses exactly what I want to accomplish. But in the mean time, what's the best work around while we wait for the next release?

FYI: I've come up with a solution, but I think a better one exists. I'll let the voting take care of it.


我想出了使用jQuery的这个解决方案,它在TabularInlineTabularInline

var selector = "h2:contains('TITLE_OF_INLINE_BLOCK')";
$(selector).parent().addClass("collapsed");
$(selector).append(" (<a class="collapse-toggle" id="customcollapser" href="#"> Show </a>)");
$("#customcollapser").click(function() {
    $(selector).parent().toggleClass("collapsed");
});

You can use grappelli - which supports collapsing fieldsets. It uses a solution much like the solutions mentioned above, but the javascript / coding is already done - you just have to add 'classes':(collapse closed',), to your fieldset ( see http://readthedocs.org/docs/django-grappelli/en/latest/customization.html)

for example:

class ModelOptions(admin.ModelAdmin):
    fieldsets = (
        ('', {
            'fields': ('title', 'subtitle', 'slug', 'pub_date', 'status',),
        }),
        ('Flags', {
            'classes': ('grp-collapse grp-closed',),
            'fields' : ('flag_front', 'flag_sticky', 'flag_allow_comments', 'flag_comments_closed',),
        }),
        ('Tags', {
            'classes': ('grp-collapse grp-open',),
            'fields' : ('tags',),
        }),
    )

class StackedItemInline(admin.StackedInline):
    classes = ('grp-collapse grp-open',)

class TabularItemInline(admin.TabularInline):
    classes = ('grp-collapse grp-open',)

From django 1.10, We can now add extra css classes to InlineModelAdmin as well.

A list or tuple containing extra CSS classes to apply to the fieldset that is rendered for the inlines. Defaults to None. As with classes configured in fieldsets, inlines with a collapse class will be initially collapsed and their header will have a small “show” link.

Docs

链接地址: http://www.djcxy.com/p/65414.html

上一篇: Django管理中的同一模型的多个ModelAdmins / views

下一篇: 如何将'collapse'添加到Django StackedInline中