Fix issue with validation of NullBooleanField with choices

`get_field_kwargs` function will not set `allow_null` kwarg to `True` for `models.NullBooleanField`.
But in case choices provided for this field we need to set `allow_null` to `True`, since all fields
with provided choices will be validated as `ChoiceField` (see `ModelSerializer.build_standard_field(...)`).
This commit is contained in:
Basil Dubyk 2018-11-05 19:38:05 +02:00
parent 9dfee6cd7b
commit 527411ce92

View File

@ -100,7 +100,8 @@ def get_field_kwargs(field_name, model_field):
if model_field.has_default() or model_field.blank or model_field.null:
kwargs['required'] = False
if model_field.null and not isinstance(model_field, models.NullBooleanField):
is_null_boolean_field = isinstance(model_field, models.NullBooleanField)
if (model_field.null and not is_null_boolean_field) or (model_field.choices and is_null_boolean_field):
kwargs['allow_null'] = True
if model_field.blank and (isinstance(model_field, models.CharField) or