Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group fields in Django's admin forms

I'm pretty new to Django and experimenting with it.

I've read a quite amount of docs about the framework but I could not find information on how to "group" fields in admin forms.

What I mean by grouping is having an arbitrary number of fields grouped under an arbitrary subsection (the way the subsection is represented graphically is not important).

Is it possible to "natively" do that (by natively, I mean without overriding any Admin Form)?

like image 320
Francis Straccia Avatar asked Apr 20 '16 13:04

Francis Straccia


People also ask

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

How do you make a field required in Django admin?

Making Fields Required In Django Admin In order to make the summary field required, we need to create a custom form for the Post model. I am making them on the same file you can do this on a separate forms.py file as well.

Is there a list field in Django?

These field classes are only maintained for legacy purposes. They aren't recommended as comma separation is a fragile serialization format. For new uses, you're better off using Django 3.1's JSONField that works with all database backends.


1 Answers

You can do this with fieldsets.

For example:

class MyModelAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
           'fields': ('field1', 'field2', 'field3')
        }),
        ('Advanced options', {
            'fields': ('field4', 'field5'),
        }),
    )

See the docs for more information.

like image 183
Alasdair Avatar answered Sep 20 '22 16:09

Alasdair