From 527411ce9255a255640ee04306cf7eadd47fa5c4 Mon Sep 17 00:00:00 2001 From: Basil Dubyk Date: Mon, 5 Nov 2018 19:38:05 +0200 Subject: [PATCH] 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(...)`). --- rest_framework/utils/field_mapping.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index 50de3f125..483d8375f 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -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