[1.10.x] Refs #27039 -- Fixed regression with field defaults in prefixed forms.

Backport of d9c083cfee from master
This commit is contained in:
Alex Hill 2016-09-01 13:18:41 +08:00 committed by Tim Graham
parent e19490d4b1
commit db3eabfae5
2 changed files with 17 additions and 1 deletions

View File

@ -54,7 +54,7 @@ def construct_instance(form, instance, fields=None, exclude=None):
continue continue
# Leave defaults for fields that aren't in POST data, except for # Leave defaults for fields that aren't in POST data, except for
# checkbox inputs because they don't appear in POST data if not checked. # checkbox inputs because they don't appear in POST data if not checked.
if (f.has_default() and f.name not in form.data and if (f.has_default() and form.add_prefix(f.name) not in form.data and
not getattr(form[f.name].field.widget, 'dont_use_model_field_default_for_empty_data', False)): not getattr(form[f.name].field.widget, 'dont_use_model_field_default_for_empty_data', False)):
continue continue
# Defer saving file-type fields until after the other fields, so a # Defer saving file-type fields until after the other fields, so a

View File

@ -585,6 +585,22 @@ class ModelFormBaseTest(TestCase):
self.assertIsInstance(mf1.fields['active'].widget, forms.CheckboxInput) self.assertIsInstance(mf1.fields['active'].widget, forms.CheckboxInput)
self.assertIs(m1._meta.get_field('active').get_default(), True) self.assertIs(m1._meta.get_field('active').get_default(), True)
def test_prefixed_form_with_default_field(self):
class PubForm(forms.ModelForm):
prefix = 'form-prefix'
class Meta:
model = PublicationDefaults
fields = ('mode',)
mode = 'de'
self.assertNotEqual(mode, PublicationDefaults._meta.get_field('mode').get_default())
mf1 = PubForm({'form-prefix-mode': mode})
self.assertEqual(mf1.errors, {})
m1 = mf1.save(commit=False)
self.assertEqual(m1.mode, mode)
class FieldOverridesByFormMetaForm(forms.ModelForm): class FieldOverridesByFormMetaForm(forms.ModelForm):
class Meta: class Meta: