0
0
Djangoframework~20 mins

ModelForm for model-backed forms in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ModelForm Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a ModelForm is saved without commit?
Consider a Django ModelForm instance created with commit=False in the save() method. What is the state of the returned object?
Django
form = MyModelForm(request.POST)
if form.is_valid():
    obj = form.save(commit=False)
    # What is true about obj here?
AThe object is created but not saved to the database yet.
BThe object is saved to the database immediately.
CThe object is not created at all until commit=True is used.
DThe object is deleted from the database.
Attempts:
2 left
💡 Hint
Think about what commit=False means in Django ModelForm save method.
📝 Syntax
intermediate
2:00remaining
Which ModelForm field declaration is correct for excluding a model field?
You want to create a ModelForm that excludes the created_at field from the form. Which of the following Meta class declarations is correct?
Django
class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        # Choose the correct way to exclude 'created_at'
Aexclude = ['created_at']
Bfields = ['created_at']
Cexclude = 'created_at'
Dfields = '__all__' except 'created_at'
Attempts:
2 left
💡 Hint
The exclude attribute expects a list of field names.
🔧 Debug
advanced
3:00remaining
Why does this ModelForm raise a ValidationError on save?
Given this ModelForm code, why does calling form.save() raise a ValidationError even though form.is_valid() returned True?
Django
class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['name']

form = MyModelForm(data={'name': 'Alice'})
if form.is_valid():
    form.save()
AThe form is not valid because 'name' is missing.
BThe form.save() method is called without commit=False, causing a database error.
CThe 'email' field is required by the model but missing in the data, causing save to fail.
DThe ModelForm must include all model fields in fields list.
Attempts:
2 left
💡 Hint
Check which fields are required by the model and which are provided in the form data.
state_output
advanced
2:00remaining
What is the value of form.errors after invalid submission?
A ModelForm is submitted with missing required fields. What does form.errors contain after calling form.is_valid()?
Django
form = MyModelForm(data={'name': ''})
valid = form.is_valid()
errors = form.errors
A{}
B{'name': ['This field is required.']}
CNone
D{'non_field_errors': ['Invalid data']}
Attempts:
2 left
💡 Hint
What does Django do when a required field is empty?
🧠 Conceptual
expert
3:00remaining
How does Django ModelForm handle related model fields by default?
When a Django ModelForm is generated for a model with a ForeignKey field, how is that field represented in the form by default?
AIt is excluded from the form automatically.
BAs a text input field expecting the related model's primary key.
CAs a hidden input field with the related model's ID.
DAs a dropdown select field listing related model instances.
Attempts:
2 left
💡 Hint
Think about how Django lets users pick related objects in forms.