0
0
Djangoframework~20 mins

Fieldsets for form layout in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fieldsets Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Django admin fieldsets configuration?

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?

AAll fields displayed in one box without any labels.
BTwo separate boxes labeled 'Main Info' and 'Details', each containing their respective fields.
COnly the 'Main Info' fields are shown; 'Details' fields are hidden.
DFields are displayed in alphabetical order ignoring the fieldsets.
Attempts:
2 left
💡 Hint

Fieldsets group fields visually with titles in Django admin.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this fieldsets definition

Which option contains a syntax error in defining Django admin fieldsets?

fieldsets = [
    ('Info', {'fields': ['name', 'email']}),
    ('Extra', {'fields': 'age', 'gender'}),
]
AThe tuple labels must be integers, not strings.
BThe first tuple is missing a comma after 'email'.
CThe second tuple's 'fields' value should be a list, not separate strings.
DFieldsets must be a dictionary, not a list.
Attempts:
2 left
💡 Hint

Check the type of the 'fields' value in each fieldset.

state_output
advanced
2:00remaining
What happens if a field in fieldsets does not exist in the model?

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?

AThe 'phone' field is silently ignored and not displayed.
BThe admin form crashes with a server 500 error unrelated to fieldsets.
CThe form loads but the 'phone' field is shown as an empty input.
DDjango admin raises a FieldError indicating the field 'phone' does not exist.
Attempts:
2 left
💡 Hint

Django admin validates fields listed in fieldsets against the model fields.

🔧 Debug
advanced
2:00remaining
Why does this fieldsets config cause a form layout issue?

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?

ABecause an empty string '' is included as a field name, which is invalid and creates a blank space.
BBecause 'summary' is not a valid field in the model.
CBecause fieldsets must not have more than one tuple.
DBecause the field names must be tuples, not lists.
Attempts:
2 left
💡 Hint

Check the list of fields for any invalid entries.

🧠 Conceptual
expert
2:00remaining
How can you make a Django admin fieldset collapsible?

You want to make a fieldset in Django admin that can be expanded or collapsed by the user. Which option correctly achieves this?

AAdd 'classes': ['collapse'] to the fieldset dictionary, like ('Advanced', {'fields': [...], 'classes': ['collapse']})
BWrap the fieldset fields in a <details> HTML tag in the template.
CUse JavaScript to hide and show the fieldset manually; Django admin has no built-in support.
DSet 'collapsible': True in the ModelAdmin options.
Attempts:
2 left
💡 Hint

Django admin supports CSS classes on fieldsets for styling and behavior.