Rectangle 27 0

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


(function($) {
    $(document).ready(function() {
        $('fieldset.collapse.open').removeClass('collapsed');
    });
})(django.jQuery);
class PageAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': ('title', 'content', )
        }),
        ('Other Informations', {
            'classes': ('collapse', 'open'),
            'fields': ('slug', 'create-date',)
        }),
    )
{% 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 %}
Note
Rectangle 27 0

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


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

Accoding with the grappelli docs only need to add "classes" : ('grp-collapse grp-closed')

note: if you use grappelli version < 2.4 use 'grp-closed' instead 'collapse-closed'* actually 'collapse-close' still is working but is recommended to use the new classes

Note
Rectangle 27 0

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


admin/(app)/(model)/change_form.html

call the click method of the field-set collapser

instead of removing the collapsible effect

Starting from Setomidor answer, I'd like to suggest a simpler alternative that does exactly what you want (if Grappelli is not an option, obviously).

Note
Rectangle 27 0

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


Thanks, I'll probably end up going with that. We'd used grappelli on a couple other projects, and I knew it could do this, just didn't think of it at the time.

Note
Rectangle 27 0

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


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

{% block extrahead %}
    <!-- Load superblock (where django.jquery resides) -->
    {{ block.super }}
    <!-- only do this on 'add' actions (and not 'change' actions) -->
    {% if add and adminform %}
    <script type="text/javascript">
        (function($) {
            $(document).ready(function($) {
                //Remove the 'collapsed' class to make the fieldset open
                $('.collapse').removeClass('collapsed');

                //Remove the show/hide links
                $('.collapse h2 a').remove();

                //Tidy up by removing the parenthesis from all headings
                var $headings = $(".collapse h2");
                $headings.each(function(i, current){
                    var str = $(current).text();
                    parenthesisStart = str.indexOf('(');
                    $(current).text(str.substring(0, parenthesisStart));
                });
            });                                 
        })(django.jQuery);                      
    </script>
    {% endif %}
{% endblock %}

Old question, but I ran into the same one and came up with an answer which can be implemented using standard django:

create file: admin/(app)/(model)/change_form.html in your templates directory to make your (model) of your (app) use that form file.

Note