Skip to content

Commit ea98de9

Browse files
committed
Model fields with .blank or .null now map to required=False. Closes #2017. Closes #2021.
1 parent 9e75c4d commit ea98de9

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

docs/topics/3.0-announcement.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,9 @@ We now use the following:
506506

507507
REST framework now has more explicit and clear control over validating empty values for fields.
508508

509-
Previously the meaning of the `required=False` keyword argument was underspecified. In practice its use meant that a field could either be not included in the input, or it could be included, but be `None`.
509+
Previously the meaning of the `required=False` keyword argument was underspecified. In practice its use meant that a field could either be not included in the input, or it could be included, but be `None` or the empty string.
510510

511-
We now have a better separation, with separate `required` and `allow_none` arguments.
511+
We now have a better separation, with separate `required`, `allow_none` and `allow_blank` arguments.
512512

513513
The following set of arguments are used to control validation of empty values:
514514

@@ -519,7 +519,7 @@ The following set of arguments are used to control validation of empty values:
519519

520520
Typically you'll want to use `required=False` if the corresponding model field has a default value, and additionally set either `allow_none=True` or `allow_blank=True` if required.
521521

522-
The `default` argument is there if you need it, but you'll more typically want defaults to be set on model fields, rather than serializer fields.
522+
The `default` argument is also available and always implies that the field is not required to be in the input. It is unnecessary to use the `required` argument when a default is specified, and doing so will result in an error.
523523

524524
#### Coercing output types.
525525

rest_framework/utils/field_mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_field_kwargs(field_name, model_field):
8888
kwargs['read_only'] = True
8989
return kwargs
9090

91-
if model_field.has_default():
91+
if model_field.has_default() or model_field.blank or model_field.null:
9292
kwargs['required'] = False
9393

9494
if model_field.flatchoices:

tests/test_model_serializer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class Meta:
9090
email_field = EmailField(max_length=100)
9191
float_field = FloatField()
9292
integer_field = IntegerField()
93-
null_boolean_field = NullBooleanField()
93+
null_boolean_field = NullBooleanField(required=False)
9494
positive_integer_field = IntegerField()
9595
positive_small_integer_field = IntegerField()
9696
slug_field = SlugField(max_length=100)
@@ -112,8 +112,8 @@ class Meta:
112112
id = IntegerField(label='ID', read_only=True)
113113
value_limit_field = IntegerField(max_value=10, min_value=1)
114114
length_limit_field = CharField(max_length=12, min_length=3)
115-
blank_field = CharField(allow_blank=True, max_length=10)
116-
null_field = IntegerField(allow_null=True)
115+
blank_field = CharField(allow_blank=True, max_length=10, required=False)
116+
null_field = IntegerField(allow_null=True, required=False)
117117
default_field = IntegerField(required=False)
118118
descriptive_field = IntegerField(help_text='Some help text', label='A label')
119119
choices_field = ChoiceField(choices=[('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')])

0 commit comments

Comments
 (0)