Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using fieldsets in a Django admin form?
easy
A. To change the database schema automatically
B. To group related form fields under a common heading for better organization
C. To add validation rules to form fields
D. To create new model instances

Solution

  1. Step 1: Understand what fieldsets do in Django admin

    fieldsets are used to group form fields visually under headings to make forms easier to read and organize.
  2. Step 2: Differentiate from other form features

    Validation rules and database schema changes are handled elsewhere, not by fieldsets.
  3. Final Answer:

    To group related form fields under a common heading for better organization -> Option B
  4. Quick Check:

    Fieldsets group fields = C [OK]
Hint: Fieldsets organize fields visually, not change data or validation [OK]
Common Mistakes:
  • Confusing fieldsets with validation or model changes
  • Thinking fieldsets create new data entries
  • Assuming fieldsets affect database schema
2. Which of the following is the correct syntax to define fieldsets in a Django admin class?
easy
A. fieldsets = [("Section", ["name", "email"])]
B. fieldsets = {"Section": ["name", "email"]}
C. fieldsets = {"fields": ("name", "email")}
D. fieldsets = [("Section", {"fields": ("name", "email")})]

Solution

  1. Step 1: Recall Django admin fieldsets syntax

    Fieldsets must be a list of tuples, each tuple has a title string and a dictionary with a 'fields' key holding a tuple of field names.
  2. Step 2: Check each option format

    fieldsets = [("Section", {"fields": ("name", "email")})] matches the correct pattern: list of tuples with title and dict containing 'fields' tuple. Others use wrong types or missing dict.
  3. Final Answer:

    fieldsets = [("Section", {"fields": ("name", "email")})] -> Option D
  4. Quick Check:

    Fieldsets syntax = list of (title, dict with fields) [OK]
Hint: Fieldsets = list of (title, dict with 'fields' tuple) [OK]
Common Mistakes:
  • Using dict instead of list for fieldsets
  • Missing dictionary around 'fields'
  • Using list instead of tuple for fields
3. Given this Django admin class snippet:
class MyModelAdmin(admin.ModelAdmin):
    fieldsets = [
        ("Personal Info", {"fields": ("first_name", "last_name")}),
        ("Contact", {"fields": ("email", "phone")})
    ]

What will the admin form display?
medium
A. Two sections: 'Personal Info' with first_name and last_name fields, and 'Contact' with email and phone fields
B. One section with all fields mixed together
C. Only the 'Personal Info' fields will show
D. An error because fieldsets must be a dictionary

Solution

  1. Step 1: Understand the fieldsets definition

    The fieldsets list defines two sections with titles and their respective fields grouped.
  2. Step 2: Predict form layout

    The admin form will show two separate groups labeled 'Personal Info' and 'Contact' with their fields inside.
  3. Final Answer:

    Two sections: 'Personal Info' with first_name and last_name fields, and 'Contact' with email and phone fields -> Option A
  4. Quick Check:

    Fieldsets create labeled groups = B [OK]
Hint: Fieldsets create visible groups with headings in admin form [OK]
Common Mistakes:
  • Thinking fields appear all together without groups
  • Assuming only first group shows
  • Believing fieldsets must be dict, causing error
4. Identify the error in this Django admin fieldsets code:
fieldsets = [
    ("Main", ["title", "description"]),
    ("Details", {"fields": ("date", "location")})
]
medium
A. Fieldsets must be a dictionary, not a list
B. The second tuple has fields as a tuple instead of a list
C. The first tuple uses a list instead of a dictionary for fields
D. Field names must be strings, not tuples

Solution

  1. Step 1: Check the structure of each fieldset tuple

    The first tuple has a title and a list of fields directly, but Django expects a dictionary with a 'fields' key.
  2. Step 2: Confirm correct fieldsets format

    Each tuple should have a dictionary with 'fields' key mapping to a list or tuple of field names. The second tuple is correct.
  3. Final Answer:

    The first tuple uses a list instead of a dictionary for fields -> Option C
  4. Quick Check:

    Fieldsets need dict with 'fields' key [OK]
Hint: Each fieldset tuple needs a dict with 'fields' key [OK]
Common Mistakes:
  • Using list instead of dict for fields
  • Confusing list and tuple types for fields
  • Thinking fieldsets must be dict instead of list
5. You want to create a Django admin form with three sections: 'Basic Info' (fields: name, age), 'Contact' (email, phone), and 'Preferences' (newsletter, notifications). Which fieldsets definition correctly achieves this layout?
hard
A. fieldsets = [ ("Basic Info", {"fields": ["name", "age"]}), ("Contact", {"fields": ["email", "phone"]}), ("Preferences", {"fields": ["newsletter", "notifications"]}) ]
B. fieldsets = { "Basic Info": ["name", "age"], "Contact": ["email", "phone"], "Preferences": ["newsletter", "notifications"] }
C. fieldsets = [ ("Basic Info", ["name", "age"]), ("Contact", ["email", "phone"]), ("Preferences", ["newsletter", "notifications"]) ]
D. fieldsets = [ ("Basic Info", {"fields": ("name", "age")}), ("Contact", {"fields": ("email", "phone")}), ("Preferences", {"fields": ("newsletter", "notifications")}) ]

Solution

  1. Step 1: Recall correct fieldsets structure

    Fieldsets must be a list of tuples, each with a title and a dictionary containing 'fields' key with a list or tuple of field names.
  2. Step 2: Compare options for correct syntax and data types

    fieldsets = [ ("Basic Info", {"fields": ["name", "age"]}), ("Contact", {"fields": ["email", "phone"]}), ("Preferences", {"fields": ["newsletter", "notifications"]}) ] uses list for fields inside the dict, which is valid. fieldsets = [ ("Basic Info", {"fields": ("name", "age")}), ("Contact", {"fields": ("email", "phone")}), ("Preferences", {"fields": ("newsletter", "notifications")}) ] uses tuple for fields, also valid but less common. fieldsets = { "Basic Info": ["name", "age"], "Contact": ["email", "phone"], "Preferences": ["newsletter", "notifications"] } uses dict with lists but keys are titles, not tuples. fieldsets = [ ("Basic Info", ["name", "age"]), ("Contact", ["email", "phone"]), ("Preferences", ["newsletter", "notifications"]) ] uses list instead of dict for fields.
  3. Step 3: Choose best practice

    Both A and D are valid, but Django docs often show lists for fields. fieldsets = [ ("Basic Info", {"fields": ["name", "age"]}), ("Contact", {"fields": ["email", "phone"]}), ("Preferences", {"fields": ["newsletter", "notifications"]}) ] matches this and is fully correct.
  4. Final Answer:

    fieldsets with list of tuples, each tuple has title and dict with 'fields' list -> Option A
  5. Quick Check:

    Fieldsets = list of (title, dict with 'fields' list) [OK]
Hint: Use list of (title, dict with 'fields' list) for clear fieldsets [OK]
Common Mistakes:
  • Using dict with titles as keys instead of list of tuples
  • Using list instead of dict for fields
  • Mixing tuples and lists incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard