Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16869 -- BaseGenericInlineFormSet.save_new should use form's s…

…ave() method

Thanks mattaustin for the report and Pablo Recio (pyriku) for the patch.
  • Loading branch information...
commit b11564fd36587b1077bf7d77b62b7879cc08c382 1 parent c348550
Pablo Mouzo authored September 07, 2013 timgraham committed September 07, 2013
13  django/contrib/contenttypes/generic.py
@@ -415,13 +415,12 @@ def get_default_prefix(cls):
415 415
         ))
416 416
 
417 417
     def save_new(self, form, commit=True):
418  
-        kwargs = {
419  
-            self.ct_field.get_attname(): ContentType.objects.get_for_model(
420  
-                self.instance, for_concrete_model=self.for_concrete_model).pk,
421  
-            self.ct_fk_field.get_attname(): self.instance.pk,
422  
-        }
423  
-        new_obj = self.model(**kwargs)
424  
-        return save_instance(form, new_obj, commit=commit)
  418
+        setattr(form.instance, self.ct_field.get_attname(),
  419
+            ContentType.objects.get_for_model(self.instance).pk)
  420
+        setattr(form.instance, self.ct_fk_field.get_attname(),
  421
+            self.instance.pk)
  422
+        return form.save(commit=commit)
  423
+
425 424
 
426 425
 def generic_inlineformset_factory(model, form=ModelForm,
427 426
                                   formset=BaseGenericInlineFormSet,
26  tests/generic_relations/tests.py
@@ -306,6 +306,32 @@ def test_generic_inlineformset_factory(self):
306 306
         form = Formset().forms[0]
307 307
         self.assertIsInstance(form['tag'].field.widget, CustomWidget)
308 308
 
  309
+    def test_save_new_uses_form_save(self):
  310
+        """
  311
+        Regression for #16260: save_new should call form.save()
  312
+        """
  313
+        class SaveTestForm(forms.ModelForm):
  314
+            def save(self, *args, **kwargs):
  315
+                self.instance.saved_by = "custom method"
  316
+                return super(SaveTestForm, self).save(*args, **kwargs)
  317
+
  318
+        Formset = generic_inlineformset_factory(
  319
+            ForProxyModelModel, fields='__all__', form=SaveTestForm)
  320
+
  321
+        instance = ProxyRelatedModel.objects.create()
  322
+
  323
+        data = {
  324
+            'form-TOTAL_FORMS': '1',
  325
+            'form-INITIAL_FORMS': '0',
  326
+            'form-MAX_NUM_FORMS': '',
  327
+            'form-0-title': 'foo',
  328
+        }
  329
+
  330
+        formset = Formset(data, instance=instance, prefix='form')
  331
+        self.assertTrue(formset.is_valid())
  332
+        new_obj = formset.save()[0]
  333
+        self.assertEqual(new_obj.saved_by, "custom method")
  334
+
309 335
     def test_save_new_for_proxy(self):
310 336
         Formset = generic_inlineformset_factory(ForProxyModelModel,
311 337
             fields='__all__', for_concrete_model=False)

0 notes on commit b11564f

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