Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #14938 - Fixed save-as-new on inline formset with new f…

…orms.

Backport of r15306 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c09249731b0e66bfee72e978e0274d698bfad091 1 parent 131d83b
Carl Meyer authored January 25, 2011
4  django/forms/models.py
@@ -701,10 +701,6 @@ def initial_form_count(self):
701 701
             return 0
702 702
         return super(BaseInlineFormSet, self).initial_form_count()
703 703
 
704  
-    def total_form_count(self):
705  
-        if self.save_as_new:
706  
-            return super(BaseInlineFormSet, self).initial_form_count()
707  
-        return super(BaseInlineFormSet, self).total_form_count()
708 704
 
709 705
     def _construct_form(self, i, **kwargs):
710 706
         form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)
10  tests/regressiontests/model_formsets_regress/models.py
@@ -17,3 +17,13 @@ class Restaurant(Place):
17 17
 class Manager(models.Model):
18 18
     retaurant = models.ForeignKey(Restaurant)
19 19
     name = models.CharField(max_length=50)
  20
+
  21
+class Network(models.Model):
  22
+    name = models.CharField(max_length=15)
  23
+
  24
+class Host(models.Model):
  25
+    network = models.ForeignKey(Network)
  26
+    hostname = models.CharField(max_length=25)
  27
+
  28
+    def __unicode__(self):
  29
+        return self.hostname
35  tests/regressiontests/model_formsets_regress/tests.py
@@ -2,7 +2,7 @@
2 2
 from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory
3 3
 from django.test import TestCase
4 4
 
5  
-from models import User, UserSite, Restaurant, Manager
  5
+from models import User, UserSite, Restaurant, Manager, Network, Host
6 6
 
7 7
 
8 8
 class InlineFormsetTests(TestCase):
@@ -159,6 +159,39 @@ def test_formset_with_none_instance(self):
159 159
         form = Form(instance=None)
160 160
         formset = FormSet(instance=None)
161 161
 
  162
+    def test_save_as_new_with_new_inlines(self):
  163
+        """
  164
+        Existing and new inlines are saved with save_as_new.
  165
+
  166
+        Regression for #14938.
  167
+
  168
+        """
  169
+        efnet = Network.objects.create(name="EFNet")
  170
+        host1 = Host.objects.create(hostname="irc.he.net", network=efnet)
  171
+
  172
+        HostFormSet = inlineformset_factory(Network, Host)
  173
+
  174
+        # Add a new host, modify previous host, and save-as-new
  175
+        data = {
  176
+            'host_set-TOTAL_FORMS': u'2',
  177
+            'host_set-INITIAL_FORMS': u'1',
  178
+            'host_set-MAX_NUM_FORMS': u'0',
  179
+            'host_set-0-id': unicode(host1.id),
  180
+            'host_set-0-hostname': u'tranquility.hub.dal.net',
  181
+            'host_set-1-hostname': u'matrix.de.eu.dal.net'
  182
+        }
  183
+
  184
+        # To save a formset as new, it needs a new hub instance
  185
+        dalnet = Network.objects.create(name="DALnet")
  186
+        formset = HostFormSet(data, instance=dalnet, save_as_new=True)
  187
+
  188
+        self.assertTrue(formset.is_valid())
  189
+        formset.save()
  190
+        self.assertQuerysetEqual(
  191
+            dalnet.host_set.order_by("hostname"),
  192
+            ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"]
  193
+            )
  194
+
162 195
 
163 196
 class CustomWidget(forms.CharField):
164 197
     pass

0 notes on commit c092497

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