Author: PaulM Date: 2011-09-21 21:52:43 -0700 (Wed, 21 Sep 2011) New Revision: 16875
Modified: django/trunk/docs/topics/forms/modelforms.txt Log: Fixed 11674 -- Clarified docs on excluded fields of ModelForms. Modified: django/trunk/docs/topics/forms/modelforms.txt =================================================================== --- django/trunk/docs/topics/forms/modelforms.txt 2011-09-22 04:36:15 UTC (rev 16874) +++ django/trunk/docs/topics/forms/modelforms.txt 2011-09-22 04:52:43 UTC (rev 16875) @@ -331,14 +331,18 @@ .. 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) form.save() @@ -632,6 +636,12 @@ 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 -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.