Author: adrian
Date: 2007-01-27 22:56:54 -0600 (Sat, 27 Jan 2007)
New Revision: 4439

Modified:
   django/trunk/django/newforms/models.py
   django/trunk/tests/modeltests/model_forms/models.py
Log:
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

Modified: django/trunk/django/newforms/models.py
===================================================================
--- django/trunk/django/newforms/models.py      2007-01-27 22:16:16 UTC (rev 
4438)
+++ django/trunk/django/newforms/models.py      2007-01-28 04:56:54 UTC (rev 
4439)
@@ -15,10 +15,7 @@
     """
     if self.errors:
         raise ValueError("The %s could not be created because the data didn't 
validate." % self._model._meta.object_name)
-    obj = self._model(**self.clean_data)
-    if commit:
-        obj.save()
-    return obj
+    return save_instance(self, self._model(), commit)
 
 def save_instance(form, instance, commit=True):
     """
@@ -33,12 +30,18 @@
     if form.errors:
         raise ValueError("The %s could not be changed because the data didn't 
validate." % opts.object_name)
     clean_data = form.clean_data
-    for f in opts.fields + opts.many_to_many:
+    for f in opts.fields:
         if isinstance(f, models.AutoField):
             continue
         setattr(instance, f.attname, clean_data[f.name])
     if commit:
         instance.save()
+        for f in opts.many_to_many:
+            setattr(instance, f.attname, getattr(instance, 
f.attname).model.objects.filter(pk__in = clean_data[f.name]))
+    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many
+    # data will be lost. This happens because a many-to-many options cannot be
+    # set on an object until after it's saved. Maybe we should raise an
+    # exception in that case.
     return instance
 
 def make_instance_save(instance):

Modified: django/trunk/tests/modeltests/model_forms/models.py
===================================================================
--- django/trunk/tests/modeltests/model_forms/models.py 2007-01-27 22:16:16 UTC 
(rev 4438)
+++ django/trunk/tests/modeltests/model_forms/models.py 2007-01-28 04:56:54 UTC 
(rev 4439)
@@ -224,6 +224,47 @@
 <option value="3">Third test</option>
 </select></li>
 
+>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': 
u'1988-01-04',
+...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']})
+>>> new_art = f.save()
+>>> new_art.id
+1
+>>> new_art = Article.objects.get(id=1)
+>>> new_art.categories.all()
+[<Category: Entertainment>, <Category: It's a test>]
+
+Now, submit form data with no categories. This deletes the existing categories.
+>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': 
u'1988-01-04',
+...     'writer': u'1', 'article': u'Hello.'})
+>>> new_art = f.save()
+>>> new_art.id
+1
+>>> new_art = Article.objects.get(id=1)
+>>> new_art.categories.all()
+[]
+
+Create a new article, with categories, via the form.
+>>> ArticleForm = form_for_model(Article)
+>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': 
u'1967-11-01',
+...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']})
+>>> new_art = f.save()
+>>> new_art.id
+2
+>>> new_art = Article.objects.get(id=2)
+>>> new_art.categories.all()
+[<Category: Entertainment>, <Category: It's a test>]
+
+Create a new article, with no categories, via the form.
+>>> ArticleForm = form_for_model(Article)
+>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': 
u'1967-11-01',
+...     'writer': u'1', 'article': u'Test.'})
+>>> new_art = f.save()
+>>> new_art.id
+3
+>>> new_art = Article.objects.get(id=3)
+>>> new_art.categories.all()
+[]
+
 Here, we define a custom Form. Because it happens to have the same fields as
 the Category model, we can use save_instance() to apply its changes to an
 existing Category instance.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to