Fixed #16869: BaseGenericInlineFormSet.save_new should use form's save() method #1599

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants

@timgraham timgraham and 1 other commented on an outdated diff Sep 7, 2013

tests/generic_relations/tests.py
+ def test_save_new_uses_form_save(self):
+ """
+ Regression for #16260: save_new should call form's save
+ method.
+ """
+
+ # Custom form used to check save was called
+ class SaveTestForm(forms.ModelForm):
+ save_called = False
+
+ @classmethod
+ def __saved(cls):
+ cls.save_called = True
+
+ def save(self, *args, **kwargs):
+ SaveTestForm.__saved()
@timgraham

timgraham Sep 7, 2013

Owner

IMO it would be simpler if we just added some custom logic here like setting an attribute on the model to a certain value

@polmuz

polmuz Sep 7, 2013

Contributor

you are right!

@timgraham timgraham commented on the diff Sep 7, 2013

tests/generic_relations/tests.py
+ super(SaveTestForm, self).save(*args, **kwargs)
+
+ Formset = generic_inlineformset_factory(ForProxyModelModel,
+ fields='__all__', for_concrete_model=True,
+ form=SaveTestForm)
+
+ instance = ProxyRelatedModel.objects.create()
+
+ data = {
+ 'form-TOTAL_FORMS': '1',
+ 'form-INITIAL_FORMS': '0',
+ 'form-MAX_NUM_FORMS': '',
+ 'form-0-title': 'foo',
+ }
+
+ formset = Formset(data, instance=instance, prefix='form')
@timgraham

timgraham Sep 7, 2013

Owner

don't think prefix needs to be specified

@polmuz

polmuz Sep 7, 2013

Contributor

it fails if I remove the prefix ManagementForm data is missing or has been tampered with

@timgraham timgraham and 1 other commented on an outdated diff Sep 7, 2013

tests/generic_relations/tests.py
+ fields='__all__', for_concrete_model=True,
+ form=SaveTestForm)
+
+ instance = ProxyRelatedModel.objects.create()
+
+ data = {
+ 'form-TOTAL_FORMS': '1',
+ 'form-INITIAL_FORMS': '0',
+ 'form-MAX_NUM_FORMS': '',
+ 'form-0-title': 'foo',
+ }
+
+ formset = Formset(data, instance=instance, prefix='form')
+ self.assertTrue(formset.is_valid())
+
+ new_obj, = formset.save()
@timgraham

timgraham Sep 7, 2013

Owner

extra comma?

@polmuz

polmuz Sep 7, 2013

Contributor

I stole this code from another test, it's an unboxing trick to get the first item of a one item list, I'm changing it to use formset.save()[0]

@timgraham timgraham commented on an outdated diff Sep 7, 2013

tests/generic_relations/tests.py
+ """
+
+ # Custom form used to check save was called
+ class SaveTestForm(forms.ModelForm):
+ save_called = False
+
+ @classmethod
+ def __saved(cls):
+ cls.save_called = True
+
+ def save(self, *args, **kwargs):
+ SaveTestForm.__saved()
+ super(SaveTestForm, self).save(*args, **kwargs)
+
+ Formset = generic_inlineformset_factory(ForProxyModelModel,
+ fields='__all__', for_concrete_model=True,
@timgraham

timgraham Sep 7, 2013

Owner

for_concrete_model doesn't need to be specified (it defaults to True)

Owner

timgraham commented Sep 8, 2013

merged in b11564f - thanks!

timgraham closed this Sep 8, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment