From 325dd0befea3012c42eefa061f509fbdf1b6a8aa Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 11 Aug 2016 15:18:48 -0400 Subject: [1.10.x] Fixed #27039 -- Fixed empty data fallback to model field default in model forms. Backport of 4bc6b939944183533ae74791d21282e613f63a96 from master --- django/forms/models.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'django/forms/models.py') diff --git a/django/forms/models.py b/django/forms/models.py index 15e251abe7..b3aab63124 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -52,6 +52,11 @@ def construct_instance(form, instance, fields=None, exclude=None): continue if exclude and f.name in exclude: 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 + 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 # callable upload_to can use the values from other fields. if isinstance(f, models.FileField): -- cgit v1.3