Skip to content

Commit

Permalink
[1.3.X] Fixed #11674 -- Clarified docs on excluded fields of ModelFor…
Browse files Browse the repository at this point in the history
…ms. Thanks PieterSwinkels for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.3.X@16881 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
PaulM authored and PaulM committed Sep 22, 2011
1 parent d65d17f commit 7abc6d1
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions docs/topics/forms/modelforms.txt
Expand Up @@ -332,13 +332,17 @@ Since the Author model has only 3 fields, 'name', 'title', and
.. note::

If you specify ``fields`` or ``exclude`` when creating a form with
``ModelForm``, then the fields that are not in the resulting form will not
be set by the form's ``save()`` method. Django will prevent any attempt to
save an incomplete model, so if the model does not allow the missing fields
to be empty, and does not provide a default value for the missing fields,
any attempt to ``save()`` a ``ModelForm`` with missing fields will fail.
To avoid this failure, you must instantiate your model with initial values
for the missing, but required fields::
``ModelForm``, then the fields that are not in the resulting form
will not be set by the form's ``save()`` method. Also, if you
manually add the excluded fields back to the form, they will not
be initialized from the model instance.

Django will prevent any attempt to save an incomplete model, so if
the model does not allow the missing fields to be empty, and does
not provide a default value for the missing fields, any attempt to
``save()`` a ``ModelForm`` with missing fields will fail. To
avoid this failure, you must instantiate your model with initial
values for the missing, but required fields::

author = Author(title='Mr')
form = PartialAuthorForm(request.POST, instance=author)
Expand Down Expand Up @@ -633,6 +637,12 @@ database. If a given instance's data didn't change in the bound data, the
instance won't be saved to the database and won't be included in the return
value (``instances``, in the above example).

When fields are missing from the form (for example because they have
been excluded), these fields will not be set by the ``save()``
method. You can find more information about this restriction, which
also holds for regular ``ModelForms``, in `Using a subset of fields on
the form`_.

Pass ``commit=False`` to return the unsaved model instances::

# don't save to the database
Expand Down

0 comments on commit 7abc6d1

Please sign in to comment.