Skip to content

Commit

Permalink
[1.10.x] Refs #27039 -- Fixed regression with field defaults in prefi…
Browse files Browse the repository at this point in the history
…xed forms.

Backport of d9c083c from master
  • Loading branch information
AlexHill authored and timgraham committed Sep 1, 2016
1 parent e19490d commit db3eabf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/forms/models.py
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/model_forms/tests.py
Expand Up @@ -585,6 +585,22 @@ class Meta:
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:
Expand Down

0 comments on commit db3eabf

Please sign in to comment.