Skip to content

Commit d3fd8a1

Browse files
committed
Fixed django#15591 - Clarified interaction between ModelForm and model validation.
1 parent 78f6669 commit d3fd8a1

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

docs/ref/models/instances.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ Validating objects
6767

6868
There are three steps involved in validating a model:
6969

70-
1. Validate the model fields
71-
2. Validate the model as a whole
72-
3. Validate the field uniqueness
70+
1. Validate the model fields - :meth:`Model.clean_fields()`
71+
2. Validate the model as a whole - :meth:`Model.clean()`
72+
3. Validate the field uniqueness - :meth:`Model.validate_unique()`
7373

7474
All three steps are performed when you call a model's
7575
:meth:`~Model.full_clean()` method.
@@ -97,17 +97,20 @@ not be corrected by the user.
9797

9898
Note that ``full_clean()`` will *not* be called automatically when you call
9999
your model's :meth:`~Model.save()` method, nor as a result of
100-
:class:`~django.forms.ModelForm` validation. You'll need to call it manually
101-
when you want to run one-step model validation for your own manually created
102-
models.
100+
:class:`~django.forms.ModelForm` validation. In the case of
101+
:class:`~django.forms.ModelForm` validation, :meth:`Model.clean_fields()`,
102+
:meth:`Model.clean()`, and :meth:`Model.validate_unique()` are all called
103+
individually.
103104

104-
Example::
105+
You'll need to call ``full_clean`` manually when you want to run one-step model
106+
validation for your own manually created models. For example::
105107

106108
try:
107109
article.full_clean()
108110
except ValidationError as e:
109111
# Do something based on the errors contained in e.message_dict.
110112
# Display them to a user, or handle them programatically.
113+
pass
111114

112115
The first step ``full_clean()`` performs is to clean each individual field.
113116

docs/topics/forms/modelforms.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ we'll discuss in a moment.)::
192192
name = forms.CharField(max_length=100)
193193
authors = forms.ModelMultipleChoiceField(queryset=Author.objects.all())
194194

195+
.. _modelform-is-valid-and-errors:
196+
195197
The ``is_valid()`` method and ``errors``
196198
----------------------------------------
197199

@@ -213,7 +215,9 @@ method. This method creates and saves a database object from the data
213215
bound to the form. A subclass of ``ModelForm`` can accept an existing
214216
model instance as the keyword argument ``instance``; if this is
215217
supplied, ``save()`` will update that instance. If it's not supplied,
216-
``save()`` will create a new instance of the specified model::
218+
``save()`` will create a new instance of the specified model:
219+
220+
.. code-block:: python
217221

218222
# Create a form instance from POST data.
219223
>>> f = ArticleForm(request.POST)
@@ -232,8 +236,10 @@ supplied, ``save()`` will update that instance. If it's not supplied,
232236
>>> f = ArticleForm(request.POST, instance=a)
233237
>>> f.save()
234238

235-
Note that ``save()`` will raise a ``ValueError`` if the data in the form
236-
doesn't validate -- i.e., if form.errors evaluates to True.
239+
Note that if the form :ref:`hasn't been validated
240+
<modelform-is-valid-and-errors>`, calling ``save()`` will do so by checking
241+
``form.errors``. A ``ValueError`` will be raised if the data in the form
242+
doesn't validate -- i.e., if ``form.errors`` evaluates to ``True``.
237243

238244
This ``save()`` method accepts an optional ``commit`` keyword argument, which
239245
accepts either ``True`` or ``False``. If you call ``save()`` with

0 commit comments

Comments
 (0)