diff options
| author | Tim Graham <timograham@gmail.com> | 2016-08-11 15:18:48 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-08-24 19:37:04 -0400 |
| commit | 325dd0befea3012c42eefa061f509fbdf1b6a8aa (patch) | |
| tree | 87d026fd736c1f8985cb1882e6a42e4bf7e76776 /django | |
| parent | c4ee93128f1880c3993cf07619ff4e0524920a8e (diff) | |
[1.10.x] Fixed #27039 -- Fixed empty data fallback to model field default in model forms.
Backport of 4bc6b939944183533ae74791d21282e613f63a96 from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/forms/models.py | 5 | ||||
| -rw-r--r-- | django/forms/widgets.py | 4 |
2 files changed, 9 insertions, 0 deletions
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): diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 5eb6ed089b..735207aa80 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -480,6 +480,10 @@ def boolean_check(v): class CheckboxInput(Widget): + # Don't use model field defaults for fields that aren't in POST data, + # because checkboxes don't appear in POST data if not checked. + dont_use_model_field_default_for_empty_data = True + def __init__(self, attrs=None, check_test=None): super(CheckboxInput, self).__init__(attrs) # check_test is a callable that takes a value and returns True |
