Edited docs/newforms.txt changes from [5804]

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5808 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2007-08-06 04:52:14 +00:00
parent 1474a5fd2c
commit 89d4a56594
1 changed files with 17 additions and 9 deletions

View File

@ -1511,26 +1511,34 @@ exists in the database.
To work around this problem, every time you save a form using ``commit=False``, To work around this problem, every time you save a form using ``commit=False``,
Django adds a ``save_m2m()`` method to the form created by ``form_for_model``. Django adds a ``save_m2m()`` method to the form created by ``form_for_model``.
After you have manually saved the instance produced by the form, you can invoke After you've manually saved the instance produced by the form, you can invoke
``save_m2m()`` to save the many-to-many form data:: ``save_m2m()`` to save the many-to-many form data. For example::
# Create a form instance with POST data. # Create a form instance with POST data.
>>> f = AuthorForm(request.POST) >>> f = AuthorForm(request.POST)
# Create, but don't save the new author instance # Create, but don't save the new author instance.
>>> new_author = f.save(commit=False) >>> new_author = f.save(commit=False)
# Modify the author in some way # Modify the author in some way.
... >>> new_author.some_field = 'some_value'
# Save the new instance
# Save the new instance.
>>> new_author.save() >>> new_author.save()
# Now save the many-to-many data for the form # Now, save the many-to-many data for the form.
>>> f.save_m2m() >>> f.save_m2m()
Calling ``save_m2m()`` is only required if you use ``save(commit=False)``. Calling ``save_m2m()`` is only required if you use ``save(commit=False)``.
When you use a simple ``save()`` on a form, all data - include When you use a simple ``save()`` on a form, all data -- including
many-to-many data - is saved without the need for any additional method calls. many-to-many data -- is saved without the need for any additional method calls.
For example::
# Create a form instance with POST data.
>>> f = AuthorForm(request.POST)
# Create and save the new author instance. There's no need to do anything else.
>>> new_author = f.save()
Using an alternate base class Using an alternate base class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~