There is reasonably precise documentation about Django Form validation and I have used it successfully already, so what is my problem?
My problem is remembering this stuff.
The framework involves redundancy, irregularity, some non-obvious names, and of course a lot of behind-the-scenes magic and I do not seem to be able to keep it in my head.
Can somebody help with a description that is faster to scan than the original documentation?
Assume you have a Form class MyForm with an instance called myform
and containing various Fields, in particular a 
SomeField field called somefield which we use as an example to understand
what is going on.
SomeField can be from Django or your own code.
Form validation processThese are the validation steps that Django is going to perform or attempt:
SomeField.to_python(self, value)
myform
value to its Python target type (e.g. int)value
value coerced into the proper Python type for SomeField
ValidationError
SomeField.validate(self, value)
myform
value
ValidationError
SomeField.run_validators(self, value)
myform
myform.somefield
value
ValidationError combining all ValidationErrors from the validators into oneSomeField.clean(self, value)
myform
to_python, validate, and run_validators
value
to_python
myform.cleaned_data
ValidationError raised by the other operationsMyForm.clean_somefield(self)
myform with such a methodsomefield locallyself.cleaned_data (no longer just strings now!)somefield
myform.cleaned_data
ValidationError
Field.clean calls.MyForm.clean(self)
myform onceself.cleaned_data (no longer just strings now!)cleaned_data myform.cleaned_data
self.add_error or raising ValidationError.
The latter will end up in myform.non_field_errors().cleaned_data, as fields that did not
validate will be missing.ModelForms
Validation of a ModelForm has one more step added at the end:
myform.instance.full_clean():
 calling validation on the respective model instance (if any).And a ModelForm's clean method will also have
access to the model instance via this instance attribute.
For making myform validate just like you want, you therefore
have different possibilities:
SomeField class level, you can override 
SomeField.to_python or SomeField.validate (e.g. by subclassing)MyForm class level,
you can implement MyForm.clean_somefield or
just register a validator: 
somefield = SomeField(validators=[somevalidator]).
django.core.validators or a custom one.self as the first parameter.MyForm.clean.This validation process can be triggered in various ways:
myform.full_clean()
myform.is_valid()
myform.errors etc.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With