1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #27039 -- Fixed empty data fallback to model field default in model forms.

This commit is contained in:
Tim Graham
2016-08-11 15:18:48 -04:00
parent 426bca002c
commit 4bc6b93994
6 changed files with 69 additions and 2 deletions

View File

@@ -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):

View File

@@ -481,6 +481,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