Consider this Django admin class:
class BookAdmin(admin.ModelAdmin):
fieldsets = [
('Main Info', {'fields': ['title', 'author']}),
('Details', {'fields': ['published_date', 'isbn']}),
]What will the admin form display?
Fieldsets group fields visually with titles in Django admin.
The fieldsets attribute groups fields into labeled sections. Each tuple defines a box with a title and fields inside.
Which option contains a syntax error in defining Django admin fieldsets?
fieldsets = [
('Info', {'fields': ['name', 'email']}),
('Extra', {'fields': 'age', 'gender'}),
]Check the type of the 'fields' value in each fieldset.
The 'fields' key must have a list or tuple of field names. Writing 'fields': 'age', 'gender' is invalid syntax; it should be 'fields': ['age', 'gender'].
Given this admin class:
class AuthorAdmin(admin.ModelAdmin):
fieldsets = [
('Personal', {'fields': ['first_name', 'last_name']}),
('Contact', {'fields': ['email', 'phone']}),
]But the model Author has no phone field. What is the result when loading the admin form?
Django admin validates fields listed in fieldsets against the model fields.
If a field listed in fieldsets is not part of the model, Django admin raises a FieldError to alert the developer.
Consider this fieldsets setup:
fieldsets = [
('Main', {'fields': ['title', 'author']}),
('Extra', {'fields': ['summary', '']})
]The admin form shows an empty space where the second field should be. Why?
Check the list of fields for any invalid entries.
Including an empty string '' as a field name causes Django admin to render a blank space in the form. Field names must be valid model fields.
You want to make a fieldset in Django admin that can be expanded or collapsed by the user. Which option correctly achieves this?
Django admin supports CSS classes on fieldsets for styling and behavior.
Adding the CSS class 'collapse' to a fieldset makes it collapsible in Django admin. This is the built-in way to enable collapsible sections.