Sample questions
Question 1Easy
Which method must return True before you read form.cleaned_data?
- validate()
- is_valid()
- check()
- save()
Answer: B. is_valid()
Explanation
is_valid() runs validation and populates cleaned_data.
Step-by-step solution
- Bind data.
- Call is_valid.
- Read cleaned_data.
Why the other options are wrong
- A.
validate() is not a real method; the correct choice is is_valid(). - C.
check() is not a real method; the correct choice is is_valid(). - D. save needs validity first.
Question 2Easy
Which class builds a form directly from a model's fields?
- ModelForm
- FormModel
- ModelBuilder
- AutoForm
Answer: A. ModelForm
Explanation
ModelForm uses Meta.model and fields to generate form fields and a save() method.
Step-by-step solution
- Set Meta.model.
- Pick fields.
Why the other options are wrong
- B.
FormModel is not a Django class; the correct choice is ModelForm. - C.
ModelBuilder is not a Django class; the correct choice is ModelForm. - D.
AutoForm is not a Django class; the correct choice is ModelForm.
Question 3Easy
Which attribute of a ModelForm Meta selects included fields?
- columns
- fields
- include
- only
Answer: B. fields
Explanation
Use fields = ['title'] or '__all__'; exclude also exists.
Step-by-step solution
- Set fields.
- Form built.
Why the other options are wrong
- A.
columns is not a Meta option; the correct choice is fields. - C.
include is not a Meta option; the correct choice is fields. - D.
only is not a Meta option; the correct choice is fields.
Question 4Medium
Which method must be called before accessing form.cleaned_data?
- save()
- clean_all()
- is_valid()
- render()
Answer: C. is_valid()
Explanation
cleaned_data exists only after validation.
Step-by-step solution
- form.cleaned_data is available only after validation.
- Validation is done using the is_valid() method.
- is_valid() checks all form fields and prepares cleaned_data.
- You must call is_valid() before accessing cleaned_data.
- Thus, the correct answer is is_valid().
Why the other options are wrong
- A. save() comes after validation, and calling it does not prepare cleaned_data.
- B. clean_all() is not a Django form method; cleaning is done in clean() and clean_fieldname().
- D. render() produces HTML for display; it does not validate data or fill cleaned_data.
Tip: Always call is_valid() before accessing form.cleaned_data in Django.
Question 5Medium
What does a BoundField mean in a template as {{ form.name }}?
- A field rendered with its current data bound to the form
- The model's raw stored value, shown as plain text with no widget around it
- A database column
- A static label
Answer: A. A field rendered with its current data bound to the form
Explanation
Also {{ form.name.errors }} shows errors.
Step-by-step solution
- Access field.
- Render widget.
Why the other options are wrong
- B. It renders a widget.
- C. A BoundField is a form field bound to data, not a database column.
- D. Label is separate.
Question 6Medium
What is the main difference between Form and ModelForm?
- Form supports validation, but ModelForm does not
- ModelForm works only inside the admin site
- Form cannot render itself as HTML at all
- ModelForm builds fields from a model and can save
Answer: D. ModelForm builds fields from a model and can save
Explanation
ModelForm links to a model with Meta.model and fields.
Step-by-step solution
- Form requires manual field definition and HTML rendering.
- ModelForm automatically creates fields from a model's attributes.
- ModelForm can save data directly to the database with
save(). - Form needs extra code to handle saving data to the database.
- ModelForm simplifies form creation and data persistence.
- Final answer: ModelForm builds fields from a model and can save.
Why the other options are wrong
- A. Form does not lack validation; both validate, but ModelForm adds model-based fields and saving.
- B. ModelForm is not limited to the admin site; it works in any view.
- C. A Form can render itself as HTML, for example with as_p or as_table.
Tip: Use ModelForm when linking to a database model; use Form for custom logic.
Question 7Medium
Where do you add validation for a single field email in a Form class?
- validate_email_field(self)
- check_email()
- clean_email(self)
- email_clean_hook()
Answer: C. clean_email(self)
Explanation
Per-field hooks are named clean_<field>; clean() validates across fields.
Step-by-step solution
- Name clean_email.
- Return the value.
Why the other options are wrong
- A.
validate_email_field(self) is not the Django hook name; the correct choice is clean_email(self). - B.
check_email() is not the Django hook name; the correct choice is clean_email(self). - D.
email_clean_hook() is not the Django hook name; the correct choice is clean_email(self).
Question 8Hard
What is the naming convention for a custom per-field validation method on a form?
- validate_<fieldname>()
- clean_<fieldname>()
- check_<fieldname>()
- <fieldname>_clean()
Answer: B. clean_<fieldname>()
Explanation
Django calls clean_<field> after the field's own clean.
Step-by-step solution
- A form's per-field check is a method named after the field.
- Django calls clean_<fieldname>() after the field's own clean.
- So the naming pattern is clean_<fieldname>: option B.
Why the other options are wrong
- A. Validate_<fieldname>() is not the Django form hook name.
- C. Check_<fieldname>() is not the Django form hook name.
- D. The prefix clean_ comes first, not as a suffix.
Tip: Clean_<field> checks one field; clean() checks values across several fields.
Question 9Hard
What does form.save(commit=False) return for a ModelForm?
- A saved instance that already has a primary key assigned by the database
- A dict
- None
- An unsaved model instance you can modify before calling save()
Answer: D. An unsaved model instance you can modify before calling save()
Explanation
Handy for setting fields such as the owner from request.user.
Step-by-step solution
- commit False.
- Set field.
- Save.
Why the other options are wrong
- A. It is not saved.
- B. commit=False returns a model instance, not a dict of its values.
- C. It returns an instance.
Practise all 9 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →