Fixed #21332, #26538 -- Fixed inconsistent and duplicate form fields on inline formsets.

This commit is contained in:
David Sanders 2016-03-30 04:01:15 -07:00 committed by Tim Graham
parent e2cb1018cb
commit a5c8a6ce19
2 changed files with 37 additions and 8 deletions

View File

@ -889,6 +889,13 @@ class BaseInlineFormSet(BaseModelFormSet):
super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix,
queryset=qs, **kwargs) queryset=qs, **kwargs)
# Add the generated field to form._meta.fields if it's defined to make
# sure validation isn't skipped on that field.
if self.form._meta.fields and self.fk.name not in self.form._meta.fields:
if isinstance(self.form._meta.fields, tuple):
self.form._meta.fields = list(self.form._meta.fields)
self.form._meta.fields.append(self.fk.name)
def initial_form_count(self): def initial_form_count(self):
if self.save_as_new: if self.save_as_new:
return 0 return 0
@ -960,13 +967,6 @@ class BaseInlineFormSet(BaseModelFormSet):
form.fields[name] = InlineForeignKeyField(self.instance, **kwargs) form.fields[name] = InlineForeignKeyField(self.instance, **kwargs)
# Add the generated field to form._meta.fields if it's defined to make
# sure validation isn't skipped on that field.
if form._meta.fields:
if isinstance(form._meta.fields, tuple):
form._meta.fields = list(form._meta.fields)
form._meta.fields.append(self.fk.name)
def get_unique_error_message(self, unique_check): def get_unique_error_message(self, unique_check):
unique_check = [field for field in unique_check if field != self.fk.name] unique_check = [field for field in unique_check if field != self.fk.name]
return super(BaseInlineFormSet, self).get_unique_error_message(unique_check) return super(BaseInlineFormSet, self).get_unique_error_message(unique_check)

View File

@ -1,6 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.forms.models import inlineformset_factory from django.forms.models import ModelForm, inlineformset_factory
from django.test import TestCase, skipUnlessDBFeature from django.test import TestCase, skipUnlessDBFeature
from django.utils import six from django.utils import six
@ -176,3 +176,32 @@ class InlineFormsetFactoryTest(TestCase):
formset = PoemFormSet(data, instance=poet) formset = PoemFormSet(data, instance=poet)
self.assertFalse(formset.is_valid()) self.assertFalse(formset.is_valid())
self.assertEqual(formset.non_form_errors(), ['Please correct the duplicate data for name.']) self.assertEqual(formset.non_form_errors(), ['Please correct the duplicate data for name.'])
def test_fk_not_duplicated_in_form_fields(self):
"""
A foreign key name isn't duplicated in form._meta fields (#21332).
"""
poet = Poet.objects.create(name='test')
poet.poem_set.create(name='first test poem')
poet.poem_set.create(name='second test poem')
poet.poem_set.create(name='third test poem')
PoemFormSet = inlineformset_factory(Poet, Poem, fields=('name',), extra=0)
formset = PoemFormSet(None, instance=poet)
self.assertEqual(len(formset.forms), 3)
self.assertEqual(['name', 'poet'], PoemFormSet.form._meta.fields)
def test_fk_in_all_formset_forms(self):
"""
A foreign key field is in Meta for all forms in the formset (#26538).
"""
class PoemModelForm(ModelForm):
def __init__(self, *args, **kwargs):
assert 'poet' in self._meta.fields
super(PoemModelForm, self).__init__(*args, **kwargs)
poet = Poet.objects.create(name='test')
poet.poem_set.create(name='first test poem')
poet.poem_set.create(name='second test poem')
PoemFormSet = inlineformset_factory(Poet, Poem, form=PoemModelForm, fields=('name',), extra=0)
formset = PoemFormSet(None, instance=poet)
formset.forms # Trigger form instantiation to run the assert above.