Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin - add collapse to a fieldset, but have it start expanded

Is there a way to make a fieldset collapsible, but start expanded? When you add collapse to the fieldset classes, it gets the functionality but starts collapsed. I've taken a look at the JS that shows/hides the fieldset content, but it doesn't look like there's anything in there to do what I'd like it to, so I'm assuming I'll have to roll my own. Just wanted to check before I went through that effort.

like image 232
Alex Jillard Avatar asked Nov 19 '09 21:11

Alex Jillard


1 Answers

admin.py:

class PageAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('title', 'content', )
        }),
        ('Other Informations', {
            'classes': ('collapse', 'open'),
            'fields': ('slug', 'create-date',)
        }),
    )

templates/app_label/model_name/change_form.html:

{% extends "admin/model_name/change_form.html" %}

{% block extrahead %}
    {{ block.super }}
    <script src="{{ STATIC_URL }}admin/js/collapse-open.js" type="text/javascript"></script>
{% endblock %}

static/admin/js/collapse-open.js:

(function($) {
    $(document).ready(function() {
        $('fieldset.collapse.open').removeClass('collapsed');
    });
})(django.jQuery);
like image 101
Murat Çorlu Avatar answered Sep 22 '22 05:09

Murat Çorlu