Commit ea98de9b by Tom Christie

Model fields with .blank or .null now map to required=False. Closes #2017. Closes #2021.

parent 9e75c4d8
...@@ -506,9 +506,9 @@ We now use the following: ...@@ -506,9 +506,9 @@ We now use the following:
REST framework now has more explicit and clear control over validating empty values for fields. REST framework now has more explicit and clear control over validating empty values for fields.
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`. 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.
We now have a better separation, with separate `required` and `allow_none` arguments. We now have a better separation, with separate `required`, `allow_none` and `allow_blank` arguments.
The following set of arguments are used to control validation of empty values: The following set of arguments are used to control validation of empty values:
...@@ -519,7 +519,7 @@ The following set of arguments are used to control validation of empty values: ...@@ -519,7 +519,7 @@ The following set of arguments are used to control validation of empty values:
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. 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.
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. 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.
#### Coercing output types. #### Coercing output types.
......
...@@ -88,7 +88,7 @@ def get_field_kwargs(field_name, model_field): ...@@ -88,7 +88,7 @@ def get_field_kwargs(field_name, model_field):
kwargs['read_only'] = True kwargs['read_only'] = True
return kwargs return kwargs
if model_field.has_default(): if model_field.has_default() or model_field.blank or model_field.null:
kwargs['required'] = False kwargs['required'] = False
if model_field.flatchoices: if model_field.flatchoices:
......
...@@ -90,7 +90,7 @@ class TestRegularFieldMappings(TestCase): ...@@ -90,7 +90,7 @@ class TestRegularFieldMappings(TestCase):
email_field = EmailField(max_length=100) email_field = EmailField(max_length=100)
float_field = FloatField() float_field = FloatField()
integer_field = IntegerField() integer_field = IntegerField()
null_boolean_field = NullBooleanField() null_boolean_field = NullBooleanField(required=False)
positive_integer_field = IntegerField() positive_integer_field = IntegerField()
positive_small_integer_field = IntegerField() positive_small_integer_field = IntegerField()
slug_field = SlugField(max_length=100) slug_field = SlugField(max_length=100)
...@@ -112,8 +112,8 @@ class TestRegularFieldMappings(TestCase): ...@@ -112,8 +112,8 @@ class TestRegularFieldMappings(TestCase):
id = IntegerField(label='ID', read_only=True) id = IntegerField(label='ID', read_only=True)
value_limit_field = IntegerField(max_value=10, min_value=1) value_limit_field = IntegerField(max_value=10, min_value=1)
length_limit_field = CharField(max_length=12, min_length=3) length_limit_field = CharField(max_length=12, min_length=3)
blank_field = CharField(allow_blank=True, max_length=10) blank_field = CharField(allow_blank=True, max_length=10, required=False)
null_field = IntegerField(allow_null=True) null_field = IntegerField(allow_null=True, required=False)
default_field = IntegerField(required=False) default_field = IntegerField(required=False)
descriptive_field = IntegerField(help_text='Some help text', label='A label') descriptive_field = IntegerField(help_text='Some help text', label='A label')
choices_field = ChoiceField(choices=[('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')]) choices_field = ChoiceField(choices=[('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment