ModelForm and Validation
What a form does. It takes untrusted input, checks it, converts it to proper Python types, and hands back either clean data or a set of errors. A ModelForm derives all of that from a model you already wrote.
List your fields explicitly. fields = "__all__" is convenient and will cheerfully expose a column you never meant anyone to set. Naming them is one extra line and it is the line that stops a person editing published or author by adding a field to the submitted form.
This is not hypothetical. Anything not in the form cannot be set by the request, and anything in it can.
Where custom validation goes.
clean_<fieldname> validates one field and returns its cleaned value.
clean() validates the form as a whole, for rules involving more than one field.
After validation, read form.cleaned_data, never request.POST. The cleaned version has been checked and converted; the raw version is whatever was sent.
Mark this lesson complete
Signed in, your progress follows you to every device.