From db3eabfae5c590801a74e0b318663b6480145fb9 Mon Sep 17 00:00:00 2001 From: Alex Hill Date: Thu, 1 Sep 2016 13:18:41 +0800 Subject: [PATCH] [1.10.x] Refs #27039 -- Fixed regression with field defaults in prefixed forms. Backport of d9c083cfee853272ded14c6c87623e910c9e81c4 from master --- django/forms/models.py | 2 +- tests/model_forms/tests.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/forms/models.py b/django/forms/models.py index b3aab63124..1d54ea9676 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -54,7 +54,7 @@ def construct_instance(form, instance, fields=None, exclude=None): continue # 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. - 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)): continue # Defer saving file-type fields until after the other fields, so a diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 9d3d440fdb..593e18fdaa 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -585,6 +585,22 @@ class ModelFormBaseTest(TestCase): self.assertIsInstance(mf1.fields['active'].widget, forms.CheckboxInput) 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 Meta: