Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#25702 closed New feature (duplicate)

Port ModelAdmin.fieldset to BaseForm

Reported by: Laurent Peuch Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords: form modeladmin layout admininterface
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Laurent Peuch)

In the admin interface it is possible to control the admin forms layout using the fieldset attribute. This feature is described here: https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.fieldsets

Here is the example from the documentation (modified to display the feature that allows to display multiple fields in the same line):

from django.contrib import admin

class FlatPageAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': (('url', 'title'), ('content', 'sites'))
        }),
        ('Advanced options', {
            'classes': ('collapse',),
            'fields': ('enable_comments', 'registration_required', 'template_name')
        }),
    )

The idea of this feature would be to port this exact behavior to the standard django forms. Taking the first form example from the documentation, this could look like this:

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField(widget=forms.Textarea)
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

    fieldset = (
        (None, {
            "fields": (('sender', 'cc_myself'),),
        }),
        ("Content", {
            "fields": ("subject", "message"),
        }),
    )

Currently, there is no way to modify the form layout beside the field order. This is very frustrating because this mean that if you want modify the layout of a form, you need to explode it totally to do that or you need to use another project (like django-crispy-forms (1) or another one).

While not going to all the extend of what django-cripsy-forms or another project is doing, fieldset is an already known and used convention accross the django project and allowing it in standard forms seems like a natural and intuitive choice.

Regarding the API, I'm wondering if puting the fieldset attribute in the Meta class doesn't make more sens since it is already used (only for the ModelForm?) to specify options and this will avoid a conflict with currently existing fields named fieldset.

This would look like this:

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField(widget=forms.Textarea)
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

    class Meta:
        fieldset = (
            (None, {
                "fields": (('sender', 'cc_myself'),),
            }),
            ("Content", {
                "fields": ("subject", "message"),
            }),
        )

(1) http://django-crispy-forms.readthedocs.org/en/latest/layouts.html

Change History (2)

comment:1 by Laurent Peuch, 8 years ago

Description: modified (diff)

comment:2 by Simon Charette, 8 years ago

Resolution: duplicate
Status: newclosed

This looks like a duplicate of #6630. I have to say I agree with Carl that fieldsets are too much display logic in Python.

They make a bit more sense at the admin level since you want a quick an dirty way to order your fields without having to write a template.

In the case of forms efforts have been made to move the whole logic to the template level and eventually deprecate the as_(p|table|ul) methods which are the only methods that would rely on this option.

If you disagree with these ticket resolutions please voice your opinion on the developer mailing list. In the meanwhile I'll just mark this ticket as a duplicate.

Last edited 8 years ago by Simon Charette (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top