mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Fixed #15591 - Clarified interaction between ModelForm and model validation.
This commit is contained in:
parent
78f66691ee
commit
d3fd8a1512
2 changed files with 19 additions and 10 deletions
|
@ -67,9 +67,9 @@ Validating objects
|
|||
|
||||
There are three steps involved in validating a model:
|
||||
|
||||
1. Validate the model fields
|
||||
2. Validate the model as a whole
|
||||
3. Validate the field uniqueness
|
||||
1. Validate the model fields - :meth:`Model.clean_fields()`
|
||||
2. Validate the model as a whole - :meth:`Model.clean()`
|
||||
3. Validate the field uniqueness - :meth:`Model.validate_unique()`
|
||||
|
||||
All three steps are performed when you call a model's
|
||||
:meth:`~Model.full_clean()` method.
|
||||
|
@ -97,17 +97,20 @@ not be corrected by the user.
|
|||
|
||||
Note that ``full_clean()`` will *not* be called automatically when you call
|
||||
your model's :meth:`~Model.save()` method, nor as a result of
|
||||
:class:`~django.forms.ModelForm` validation. You'll need to call it manually
|
||||
when you want to run one-step model validation for your own manually created
|
||||
models.
|
||||
:class:`~django.forms.ModelForm` validation. In the case of
|
||||
:class:`~django.forms.ModelForm` validation, :meth:`Model.clean_fields()`,
|
||||
:meth:`Model.clean()`, and :meth:`Model.validate_unique()` are all called
|
||||
individually.
|
||||
|
||||
Example::
|
||||
You'll need to call ``full_clean`` manually when you want to run one-step model
|
||||
validation for your own manually created models. For example::
|
||||
|
||||
try:
|
||||
article.full_clean()
|
||||
except ValidationError as e:
|
||||
# Do something based on the errors contained in e.message_dict.
|
||||
# Display them to a user, or handle them programatically.
|
||||
pass
|
||||
|
||||
The first step ``full_clean()`` performs is to clean each individual field.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue