Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3263 -- newforms form_for_model() and form_for_instance() now …

…handle foreign-key and many-to-many data properly. Thanks for the patch, Jeff Hilyard

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4439 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 83768bf067e84187780fbc749295d23bc048939b 1 parent 546f16d
Adrian Holovaty authored January 28, 2007
13  django/newforms/models.py
@@ -15,10 +15,7 @@ def model_save(self, commit=True):
15 15
     """
16 16
     if self.errors:
17 17
         raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name)
18  
-    obj = self._model(**self.clean_data)
19  
-    if commit:
20  
-        obj.save()
21  
-    return obj
  18
+    return save_instance(self, self._model(), commit)
22 19
 
23 20
 def save_instance(form, instance, commit=True):
24 21
     """
@@ -33,12 +30,18 @@ def save_instance(form, instance, commit=True):
33 30
     if form.errors:
34 31
         raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name)
35 32
     clean_data = form.clean_data
36  
-    for f in opts.fields + opts.many_to_many:
  33
+    for f in opts.fields:
37 34
         if isinstance(f, models.AutoField):
38 35
             continue
39 36
         setattr(instance, f.attname, clean_data[f.name])
40 37
     if commit:
41 38
         instance.save()
  39
+        for f in opts.many_to_many:
  40
+            setattr(instance, f.attname, getattr(instance, f.attname).model.objects.filter(pk__in = clean_data[f.name]))
  41
+    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
  42
+    # data will be lost. This happens because a many-to-many options cannot be
  43
+    # set on an object until after it's saved. Maybe we should raise an
  44
+    # exception in that case.
42 45
     return instance
43 46
 
44 47
 def make_instance_save(instance):
41  tests/modeltests/model_forms/models.py
@@ -224,6 +224,47 @@ def __str__(self):
224 224
 <option value="3">Third test</option>
225 225
 </select></li>
226 226
 
  227
+>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04',
  228
+...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']})
  229
+>>> new_art = f.save()
  230
+>>> new_art.id
  231
+1
  232
+>>> new_art = Article.objects.get(id=1)
  233
+>>> new_art.categories.all()
  234
+[<Category: Entertainment>, <Category: It's a test>]
  235
+
  236
+Now, submit form data with no categories. This deletes the existing categories.
  237
+>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04',
  238
+...     'writer': u'1', 'article': u'Hello.'})
  239
+>>> new_art = f.save()
  240
+>>> new_art.id
  241
+1
  242
+>>> new_art = Article.objects.get(id=1)
  243
+>>> new_art.categories.all()
  244
+[]
  245
+
  246
+Create a new article, with categories, via the form.
  247
+>>> ArticleForm = form_for_model(Article)
  248
+>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',
  249
+...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']})
  250
+>>> new_art = f.save()
  251
+>>> new_art.id
  252
+2
  253
+>>> new_art = Article.objects.get(id=2)
  254
+>>> new_art.categories.all()
  255
+[<Category: Entertainment>, <Category: It's a test>]
  256
+
  257
+Create a new article, with no categories, via the form.
  258
+>>> ArticleForm = form_for_model(Article)
  259
+>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01',
  260
+...     'writer': u'1', 'article': u'Test.'})
  261
+>>> new_art = f.save()
  262
+>>> new_art.id
  263
+3
  264
+>>> new_art = Article.objects.get(id=3)
  265
+>>> new_art.categories.all()
  266
+[]
  267
+
227 268
 Here, we define a custom Form. Because it happens to have the same fields as
228 269
 the Category model, we can use save_instance() to apply its changes to an
229 270
 existing Category instance.

0 notes on commit 83768bf

Please sign in to comment.
Something went wrong with that request. Please try again.