Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14938 - Fixed save-as-new on inline formset with new forms.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15306 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit da771cc74c32fc9cc4760fc60047f043ef8dfcb8 1 parent 28d0a0f
Carl Meyer authored January 25, 2011
4  django/forms/models.py
@@ -690,10 +690,6 @@ def initial_form_count(self):
690 690
             return 0
691 691
         return super(BaseInlineFormSet, self).initial_form_count()
692 692
 
693  
-    def total_form_count(self):
694  
-        if self.save_as_new:
695  
-            return super(BaseInlineFormSet, self).initial_form_count()
696  
-        return super(BaseInlineFormSet, self).total_form_count()
697 693
 
698 694
     def _construct_form(self, i, **kwargs):
699 695
         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):
@@ -167,6 +167,39 @@ def test_empty_fields_on_modelformset(self):
167 167
             self.assertTrue('id' in form.fields)
168 168
             self.assertEqual(len(form.fields), 1)
169 169
 
  170
+    def test_save_as_new_with_new_inlines(self):
  171
+        """
  172
+        Existing and new inlines are saved with save_as_new.
  173
+
  174
+        Regression for #14938.
  175
+
  176
+        """
  177
+        efnet = Network.objects.create(name="EFNet")
  178
+        host1 = Host.objects.create(hostname="irc.he.net", network=efnet)
  179
+
  180
+        HostFormSet = inlineformset_factory(Network, Host)
  181
+
  182
+        # Add a new host, modify previous host, and save-as-new
  183
+        data = {
  184
+            'host_set-TOTAL_FORMS': u'2',
  185
+            'host_set-INITIAL_FORMS': u'1',
  186
+            'host_set-MAX_NUM_FORMS': u'0',
  187
+            'host_set-0-id': unicode(host1.id),
  188
+            'host_set-0-hostname': u'tranquility.hub.dal.net',
  189
+            'host_set-1-hostname': u'matrix.de.eu.dal.net'
  190
+        }
  191
+
  192
+        # To save a formset as new, it needs a new hub instance
  193
+        dalnet = Network.objects.create(name="DALnet")
  194
+        formset = HostFormSet(data, instance=dalnet, save_as_new=True)
  195
+
  196
+        self.assertTrue(formset.is_valid())
  197
+        formset.save()
  198
+        self.assertQuerysetEqual(
  199
+            dalnet.host_set.order_by("hostname"),
  200
+            ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"]
  201
+            )
  202
+
170 203
 
171 204
 class CustomWidget(forms.CharField):
172 205
     pass

0 notes on commit da771cc

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