Challenge - 5 Problems
Formset Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django formset rendering?
Given this Django formset code snippet, what HTML output will be generated for the management form part?
Django
from django import forms from django.forms import formset_factory class ItemForm(forms.Form): name = forms.CharField(max_length=100) ItemFormSet = formset_factory(ItemForm, extra=2) formset = ItemFormSet() print(formset.management_form.as_p())
Attempts:
2 left
💡 Hint
Remember that extra=2 means two empty forms are created, so TOTAL_FORMS is 2 and INITIAL_FORMS is 0.
✗ Incorrect
The management form keeps track of how many forms are in the formset. Since extra=2 and no initial data, TOTAL_FORMS is 2 and INITIAL_FORMS is 0. MIN_NUM_FORMS defaults to 0 and MAX_NUM_FORMS defaults to 1000.
❓ state_output
intermediate2:00remaining
What is the value of cleaned_data after validating this formset?
Consider this Django formset with two forms submitted with data. What will be the cleaned_data list after calling is_valid()?
Django
from django import forms from django.forms import formset_factory class ProductForm(forms.Form): product_name = forms.CharField(max_length=50) quantity = forms.IntegerField(min_value=1) ProductFormSet = formset_factory(ProductForm, extra=0) form_data = { 'form-TOTAL_FORMS': '2', 'form-INITIAL_FORMS': '0', 'form-MIN_NUM_FORMS': '0', 'form-MAX_NUM_FORMS': '1000', 'form-0-product_name': 'Apples', 'form-0-quantity': '5', 'form-1-product_name': 'Bananas', 'form-1-quantity': '0' } formset = ProductFormSet(form_data) valid = formset.is_valid() cleaned = formset.cleaned_data if valid else None print(cleaned)
Attempts:
2 left
💡 Hint
Check the quantity field validation for the second form.
✗ Incorrect
The second form has quantity=0 which is below the min_value=1, so the formset is invalid and cleaned_data is null.
📝 Syntax
advanced2:00remaining
Which option correctly creates two different formsets for two models in one view?
You want to create formsets for two different Django models, Author and Book, in the same view. Which code snippet correctly defines and initializes both formsets?
Django
from django.forms import modelformset_factory from myapp.models import Author, Book AuthorFormSet = modelformset_factory(Author, fields=('name',), extra=1) BookFormSet = modelformset_factory(Book, fields=('title',), extra=1) # Initialize formsets with POST data if request.method == 'POST': author_formset = AuthorFormSet(request.POST, prefix='authors') book_formset = BookFormSet(request.POST, prefix='books')
Attempts:
2 left
💡 Hint
When using multiple formsets in one view, each must have a unique prefix.
✗ Incorrect
Without unique prefixes, formsets will clash and data will not be assigned correctly. Option B correctly assigns distinct prefixes to each formset.
🔧 Debug
advanced2:00remaining
Why does this formset always show zero forms despite extra=3?
This code tries to create a formset with 3 extra forms but the rendered formset shows no forms. What is the cause?
Django
from django import forms from django.forms import formset_factory class TaskForm(forms.Form): task = forms.CharField(max_length=100) TaskFormSet = formset_factory(TaskForm, extra=3) formset = TaskFormSet(data=None) print(len(formset.forms))
Attempts:
2 left
💡 Hint
Check how passing data=null affects formset binding and form count.
✗ Incorrect
Passing data=null makes the formset bound but with no data, so it assumes zero forms. To get extra forms, instantiate without data argument or with empty dictionary.
🧠 Conceptual
expert2:00remaining
How does prefix affect multiple formsets in one Django template?
You have two formsets in one template: author_formset with prefix 'authors' and book_formset with prefix 'books'. What is the main reason to use prefixes here?
Attempts:
2 left
💡 Hint
Think about how HTML form input names are sent in POST requests.
✗ Incorrect
Each formset's fields must have unique names so Django can map submitted data back to the correct formset. Prefixes add a unique string to field names to avoid collisions.