mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
Fixed #10845 -- Clarified the examples for using ModelForms with fields or exclude specified. Thanks to Andrew Durdin for the suggestion.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10972 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
6c36d4c4f8
commit
6ad26e6acc
@ -323,16 +323,19 @@ Since the Author model has only 3 fields, 'name', 'title', and
|
|||||||
to be empty, and does not provide a default value for 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.
|
any attempt to ``save()`` a ``ModelForm`` with missing fields will fail.
|
||||||
To avoid this failure, you must instantiate your model with initial values
|
To avoid this failure, you must instantiate your model with initial values
|
||||||
for the missing, but required fields, or use ``save(commit=False)`` and
|
for the missing, but required fields::
|
||||||
manually set any extra required fields::
|
|
||||||
|
|
||||||
instance = Instance(required_field='value')
|
author = Author(title='Mr')
|
||||||
form = InstanceForm(request.POST, instance=instance)
|
form = PartialAuthorForm(request.POST, instance=author)
|
||||||
new_instance = form.save()
|
form.save()
|
||||||
|
|
||||||
instance = form.save(commit=False)
|
Alternatively, you can use ``save(commit=False)`` and manually set
|
||||||
instance.required_field = 'new value'
|
any extra required fields::
|
||||||
new_instance = instance.save()
|
|
||||||
|
form = PartialAuthorForm(request.POST)
|
||||||
|
author = form.save(commit=False)
|
||||||
|
author.title = 'Mr'
|
||||||
|
author.save()
|
||||||
|
|
||||||
See the `section on saving forms`_ for more details on using
|
See the `section on saving forms`_ for more details on using
|
||||||
``save(commit=False)``.
|
``save(commit=False)``.
|
||||||
@ -563,8 +566,8 @@ number of objects needed::
|
|||||||
>>> formset.initial
|
>>> formset.initial
|
||||||
[{'id': 1, 'name': u'Charles Baudelaire'}, {'id': 3, 'name': u'Paul Verlaine'}]
|
[{'id': 1, 'name': u'Charles Baudelaire'}, {'id': 3, 'name': u'Paul Verlaine'}]
|
||||||
|
|
||||||
If the value of ``max_num`` is higher than the number of objects returned, up to
|
If the value of ``max_num`` is higher than the number of objects returned, up to
|
||||||
``extra`` additional blank forms will be added to the formset, so long as the
|
``extra`` additional blank forms will be added to the formset, so long as the
|
||||||
total number of forms does not exceed ``max_num``::
|
total number of forms does not exceed ``max_num``::
|
||||||
|
|
||||||
>>> AuthorFormSet = modelformset_factory(Author, max_num=4, extra=2)
|
>>> AuthorFormSet = modelformset_factory(Author, max_num=4, extra=2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user