mirror of https://github.com/django/django.git
Refs #27039 -- Fixed regression with field defaults in prefixed forms.
This commit is contained in:
parent
ca2ccf54ff
commit
d9c083cfee
|
@ -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
|
||||
|
|
|
@ -601,6 +601,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