Am 28.08.2012 um 15:35 schrieb Melvyn Sopacua: >>>> Another question: How can I expand the right fieldset dependent of >>>> current content of my choices field (as loaded from db) while >>>> rendering the change page initially? >>> >>> Look in django/contrib/admin/options.py for get_fieldsets(). Also look >>> at this bugreport for it's limitations: >>> <https://code.djangoproject.com/ticket/18681> >>> >>> Short version: the method as it is implemented requires that you have at >>> least some declared fieldsets. >>> >>> In case you're also new to python, you should convert your declared >>> fieldsets from a tuple to a list, so it's easier to modify it. >> This method belongs to InlineModelAdmin. >> I never worked with Inlines before. It seems, they work only with parent- >> child situations (as documented). Unfortunately my form deals only with one >> model. > > Not sure why you would think that it only applies to inlines. It's a > method for ModelAdmin (shared by InlineModelAdmin through inheritance). > >>>> from django.contrib.admin import ModelAdmin No idea, what this means: >>>> ModelAdmin.get_fieldsets > <unbound method ModelAdmin.get_fieldsets> > > Try this: --- class MailboxAdmin(ModelAdmin): list_display = ('localpart', 'localdomainfk', 'type', 'aliastargetaddresses', 'created', 'updated') fieldsets = [ (None, { 'fields': (('localpart', 'localdomainfk'), 'accountfk', 'lifetime', 'type') }) ] def fieldset_section(self, title, fields, classes=None) : if classes is not None : return (title, { 'fields': fields, 'classes': classes } ) else : return (title, {'fields': fields}) def get_fieldsets(self, request, obj=None) : alias_classes = mailbox_classes = None if obj : if obj.type == 'A' : alias_classes = ('collapse',) elif obj.type in 'MS': mailbox_classes = ('collapse',) fs = self.declared_fieldsets fs.append(self.fieldset_section('Mailbox', ('aoxuserid', 'quota'), mailbox_classes)) fs.append(self.fieldset_section('Aliases', ('aliastargetaddresses',), alias_classes)) fs.append(self.fieldset_section(None, ('remarks',))) return fs
--- That is exactly your code, with the conditionals adjusted a little bit. 1st time after runserver, I get: 'Key 'aoxuserid' not found in Form' --- {% for line in fieldset %} <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %} {% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}"> --- Next time, I open this page, it renders all 3 (aliases, mailboxes and remarks) fildsets twice. That means it does not collapse at all. The number of repetitions of the 3 fieldsets increases each time I display that form. Axel --- PGP-Key:29E99DD6 ☀ +49 151 2300 9283 ☀ computing @ chaos claudius -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.