From da771cc74c32fc9cc4760fc60047f043ef8dfcb8 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Tue, 25 Jan 2011 04:52:54 +0000 Subject: [PATCH] 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 --- django/forms/models.py | 4 --- .../model_formsets_regress/models.py | 10 ++++++ .../model_formsets_regress/tests.py | 35 ++++++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index 034cb7acca..f2b73340f6 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -690,10 +690,6 @@ class BaseInlineFormSet(BaseModelFormSet): return 0 return super(BaseInlineFormSet, self).initial_form_count() - def total_form_count(self): - if self.save_as_new: - return super(BaseInlineFormSet, self).initial_form_count() - return super(BaseInlineFormSet, self).total_form_count() def _construct_form(self, i, **kwargs): form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs) diff --git a/tests/regressiontests/model_formsets_regress/models.py b/tests/regressiontests/model_formsets_regress/models.py index bd12dd6c5a..937c789504 100644 --- a/tests/regressiontests/model_formsets_regress/models.py +++ b/tests/regressiontests/model_formsets_regress/models.py @@ -17,3 +17,13 @@ class Restaurant(Place): class Manager(models.Model): retaurant = models.ForeignKey(Restaurant) name = models.CharField(max_length=50) + +class Network(models.Model): + name = models.CharField(max_length=15) + +class Host(models.Model): + network = models.ForeignKey(Network) + hostname = models.CharField(max_length=25) + + def __unicode__(self): + return self.hostname diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py index 088f08d9b1..308bccebf1 100644 --- a/tests/regressiontests/model_formsets_regress/tests.py +++ b/tests/regressiontests/model_formsets_regress/tests.py @@ -2,7 +2,7 @@ from django import forms from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory from django.test import TestCase -from models import User, UserSite, Restaurant, Manager +from models import User, UserSite, Restaurant, Manager, Network, Host class InlineFormsetTests(TestCase): @@ -167,6 +167,39 @@ class InlineFormsetTests(TestCase): self.assertTrue('id' in form.fields) self.assertEqual(len(form.fields), 1) + def test_save_as_new_with_new_inlines(self): + """ + Existing and new inlines are saved with save_as_new. + + Regression for #14938. + + """ + efnet = Network.objects.create(name="EFNet") + host1 = Host.objects.create(hostname="irc.he.net", network=efnet) + + HostFormSet = inlineformset_factory(Network, Host) + + # Add a new host, modify previous host, and save-as-new + data = { + 'host_set-TOTAL_FORMS': u'2', + 'host_set-INITIAL_FORMS': u'1', + 'host_set-MAX_NUM_FORMS': u'0', + 'host_set-0-id': unicode(host1.id), + 'host_set-0-hostname': u'tranquility.hub.dal.net', + 'host_set-1-hostname': u'matrix.de.eu.dal.net' + } + + # To save a formset as new, it needs a new hub instance + dalnet = Network.objects.create(name="DALnet") + formset = HostFormSet(data, instance=dalnet, save_as_new=True) + + self.assertTrue(formset.is_valid()) + formset.save() + self.assertQuerysetEqual( + dalnet.host_set.order_by("hostname"), + ["", ""] + ) + class CustomWidget(forms.CharField): pass