[1.10.x] Refs #27039 -- Fixed regression with field defaults in prefixed forms.
Backport of d9c083cfee
from master
This commit is contained in:
parent
e19490d4b1
commit
db3eabfae5
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue