Skip to content

Commit f96e933

Browse files
committed
Fixed #4001 -- Added dynamic save_m2m method() to forms created with form_for_model and form_for_instance on save(commit=False).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5804 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 212ee65 commit f96e933

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

django/newforms/models.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@ def save_instance(form, instance, fields=None, fail_message='saved', commit=True
3535
if fields and f.name not in fields:
3636
continue
3737
setattr(instance, f.name, cleaned_data[f.name])
38-
if commit:
39-
instance.save()
38+
# Wrap up the saving of m2m data as a function
39+
def save_m2m():
40+
opts = instance.__class__._meta
41+
cleaned_data = form.cleaned_data
4042
for f in opts.many_to_many:
4143
if fields and f.name not in fields:
4244
continue
4345
if f.name in cleaned_data:
4446
setattr(instance, f.attname, cleaned_data[f.name])
45-
# GOTCHA: If many-to-many data is given and commit=False, the many-to-many
46-
# data will be lost. This happens because a many-to-many options cannot be
47-
# set on an object until after it's saved. Maybe we should raise an
48-
# exception in that case.
47+
if commit:
48+
# If we are committing, save the instance and the m2m data immediately
49+
instance.save()
50+
save_m2m()
51+
else:
52+
# We're not committing. Add a method to the form to allow deferred
53+
# saving of m2m data
54+
form.save_m2m = save_m2m
4955
return instance
5056

5157
def make_model_save(model, fields, fail_message):

docs/newforms.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,36 @@ the database. In this case, it's up to you to call ``save()`` on the resulting
15021502
model instance. This is useful if you want to do custom processing on the
15031503
object before saving it. ``commit`` is ``True`` by default.
15041504

1505+
Another side effect of using ``commit=False`` is seen when your model has
1506+
a many-to-many relation with another model. If your model has a many-to-many
1507+
relation and you specify ``commit=False`` when you save a form, Django cannot
1508+
immediately save the form data for the many-to-many relation. This is because
1509+
it isn't possible to save many-to-many data for an instance until the instance
1510+
exists in the database.
1511+
1512+
To work around this problem, every time you save a form using ``commit=False``,
1513+
Django adds a ``save_m2m()`` method to the form created by ``form_for_model``.
1514+
After you have manually saved the instance produced by the form, you can invoke
1515+
``save_m2m()`` to save the many-to-many form data::
1516+
1517+
# Create a form instance with POST data.
1518+
>>> f = AuthorForm(request.POST)
1519+
1520+
# Create, but don't save the new author instance
1521+
>>> new_author = f.save(commit=False)
1522+
1523+
# Modify the author in some way
1524+
...
1525+
# Save the new instance
1526+
>>> new_author.save()
1527+
1528+
# Now save the many-to-many data for the form
1529+
>>> f.save_m2m()
1530+
1531+
Calling ``save_m2m()`` is only required if you use ``save(commit=False)``.
1532+
When you use a simple ``save()`` on a form, all data - include
1533+
many-to-many data - is saved without the need for any additional method calls.
1534+
15051535
Using an alternate base class
15061536
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15071537

tests/modeltests/model_forms/models.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,28 @@ def __unicode__(self):
332332
>>> new_art.categories.all()
333333
[]
334334
335+
Create a new article, with categories, via the form, but use commit=False.
336+
The m2m data won't be saved until save_m2m() is invoked on the form.
337+
>>> ArticleForm = form_for_model(Article)
338+
>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',
339+
... 'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']})
340+
>>> new_art = f.save(commit=False)
341+
342+
# Manually save the instance
343+
>>> new_art.save()
344+
>>> new_art.id
345+
4
346+
347+
# The instance doesn't have m2m data yet
348+
>>> new_art = Article.objects.get(id=4)
349+
>>> new_art.categories.all()
350+
[]
351+
352+
# Save the m2m data on the form
353+
>>> f.save_m2m()
354+
>>> new_art.categories.all()
355+
[<Category: Entertainment>, <Category: It's a test>]
356+
335357
Here, we define a custom Form. Because it happens to have the same fields as
336358
the Category model, we can use save_instance() to apply its changes to an
337359
existing Category instance.

0 commit comments

Comments
 (0)