You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NullBooleanField should use forms.NullBooleanField just like django. And BooleanField should support default=True #7722
I have reduced the issue to the simplest possible case.
I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Steps to reproduce
run shell command
django-admin startproject forboolean
cd forboolean
django-admin startapp boolean
boolean/models.py
from django.db import models
from rest_framework import serializers
from rest_framework import viewsets
from rest_framework import permissions
''' not good example but make it small '''
class TestBoolean(models.Model):
acknowledged = models.BooleanField(null=True, blank=True)
autoRenewing = models.NullBooleanField()
class TestBooleanSerializer(serializers.ModelSerializer):
class Meta:
model = TestBoolean
fields = '__all__'
class TestBooleanViewSet(viewsets.ModelViewSet):
queryset = TestBoolean.objects.all()
serializer_class = TestBooleanSerializer
permission_classes = [permissions.AllowAny]
add app in forboolean/settings.py
'rest_framework',
'boolean',
modify forboolean/urls.py
from django.urls import path, include
from boolean.models import TestBooleanViewSet
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'boolean', TestBooleanViewSet)
urlpatterns = [
path('', include(router.urls)),
]
......
WARNINGS:
boolean.TestBoolean.autoRenewing: (fields.W903) NullBooleanField is deprecated. Support for it (except in historical migrations) will be removed in Django 4.0.
HINT: Use BooleanField(null=True) instead.
......
When using HTML encoded form input be aware that omitting a value will always be treated as setting a field to False, even if it has a default=True option specified. This is because HTML checkbox inputs represent the unchecked state by omitting the value, so REST framework treats omission as if it is an empty checkbox input.
Note that Django 2.1 removed the blank kwarg from models.BooleanField. Prior to Django 2.1 models.BooleanField fields were always blank=True. Thus since Django 2.1 default serializers.BooleanField instances will be generated without the required kwarg (i.e. equivalent to required=True) whereas with previous versions of Django, default BooleanField instances will be generated with a required=False option. If you want to control this behaviour manually, explicitly declare the BooleanField on the serializer class, or use the extra_kwargs option to set the required flag.
changed the title [-]NullBooleanField in should use forms.NullBooleanField just like django. And BooleanField should support default=True[/-][+]NullBooleanField should use forms.NullBooleanField just like django. And BooleanField should support default=True[/+]on Feb 22, 2021
Thank you for the steps to reproduce that issue. Due to the title, I'm a bit confused. Which NullBooleanField do you mean?
The NullBooleanField of DjangoRestFramework is deprecated and it will be removed. Thus, it is not suggested to use. I guess, any further development on that one is not suggested too.
The NullBooleanField of Django is also deprecated as you shared the warning in your message.
In the current example that you provided, both of the fields are converted to BooleanField of DRF unless they are explicitly stated otherwise.
Even though both NullBooleanFields are deprecated, do you suggest that if a field is defined in the model as models.BooleanField(null=True) or models.NullBooleanField(), then in the browsable API, the result of the field rendering should be a dropdown like NullBooleanSelect? Is that your suggestion?
Currently, that behavior can be provided with the following change in your sample code:
And yes, it looks valid to me. I suspect that this broke for use when we moved away from using the deprecated model field, and didn't introduce some behaviour to switch between the correct form field type, depending on if the serializer field is nullable or not.
Actually it's less clear to me now if we ever used to render this correctly. We don't use Django's form rendering for the browsable API, instead we have our own templates.
Our rendering for boolean fields is determined here...
I understand. So for BooleanField, you suggest having two separate templates for null=True and null=False. What would you think of keeping only one template for BooleanField and change the input element based on the field.allow_null? Would it be not a good idea to combine that logic in one template?
Either way, would it be okay for you if I work on this issue?
I understand and I agree on that one (: So I will check the following multiple options to change this behavior:
I saw that model BooleanFields are generated as ChoiceFields if there is a choices parameter on the model field definition. I was thinking to provide a default choices tuple for yes, no, and unknown (as localized) if allow_null=True on serializer BooleanField or null=True on model BooleanField and if there is no choices provided. That way, it can be generated as a ChoiceField and rendered as select. However, there maybe some issues saving the data or loading the existing data. This is one option that I'm checking (:
# Fields with choices get coerced into `ChoiceField`
# instead of using their regular typed field.
field_class=self.serializer_choice_field
The one mentioned by you: To override the template in renderers.py. I think this one could be a clean solution. I just need to check the behavior of choices kwarg on the model field or serializer field if it is provided.
The one that I mentioned above: To combine two logic in one template but that is not my favorite right now (:
Those are the options that I could come up with. Please let me know if I'm missing anything here. Thank you for your help @tomchristie .
Okay, so "I saw that model BooleanFields are generated as ChoiceFields if there is a choices parameter on the model field definition" doesn't actually help here, because that only applies in the case where ModelSerializer is auto-generating a field. We also want things to work correctly when an explicitly listed BooleanField(allow_null=True) is included on the serializer class.
I had a little looked into drf source. because form/json data render and parser need to modify both, Maybe it take a lot patch ?
by the way, I wonder why drf donot reuse django.forms?
I had a little looked into drf source. because form/json data render and parser need to modify both
Which part of the parser needs to be modified? I could not see it.
Here, I was working on the approach of overwriting the rendering template for boolean fields with allow_null=True. I have added some tests and I was planning to add more tests for nested fields. It would be nice to get some early-feedback. That was the desired behavior, right? master...tbrknt:fix-null-boolean-rendering-7722
With the following model TestBoolean and model serializer (TestBooleanSerializer), I was able to get the screenshot above.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
From the discussion above, the agreed direction seems to be an explicit override in HTMLFormRenderer.render_field so that a BooleanField with allow_null=True renders as a select (Unknown/Yes/No) instead of a checkbox.
I noticed PR #7882 attempted this but was closed without merging, and there are no linked branches or PRs anymore. @merwok — are you still planning to revive #7882? If not, I'm happy to pick this up with a fresh PR, starting with a failing test for the rendering.
Activity
[-]NullBooleanField in should use forms.NullBooleanField just like django. And BooleanField should support default=True[/-][+]NullBooleanField should use forms.NullBooleanField just like django. And BooleanField should support default=True[/+]tbrknt commentedon Mar 9, 2021
Hello,
Thank you for the steps to reproduce that issue. Due to the title, I'm a bit confused. Which
NullBooleanFielddo you mean?NullBooleanFieldof DjangoRestFramework is deprecated and it will be removed. Thus, it is not suggested to use. I guess, any further development on that one is not suggested too.NullBooleanFieldof Django is also deprecated as you shared the warning in your message.In the current example that you provided, both of the fields are converted to
BooleanFieldof DRF unless they are explicitly stated otherwise.Even though both
NullBooleanFields are deprecated, do you suggest that if a field is defined in the model asmodels.BooleanField(null=True)ormodels.NullBooleanField(), then in the browsable API, the result of the field rendering should be a dropdown like NullBooleanSelect? Is that your suggestion?Currently, that behavior can be provided with the following change in your sample code:
So do you think that this should be the default behavior when
null=Truefor a boolean field is provided?lovelydinosaur commentedon Mar 10, 2021
So @pythonwood is referring to
django.forms.NullBooleanField, notdjango.db.models.NullBooleanField. (Yes it's easily confused).And yes, it looks valid to me. I suspect that this broke for use when we moved away from using the deprecated model field, and didn't introduce some behaviour to switch between the correct form field type, depending on if the serializer field is nullable or not.
Blergh.
lovelydinosaur commentedon Mar 10, 2021
Actually it's less clear to me now if we ever used to render this correctly. We don't use Django's form rendering for the browsable API, instead we have our own templates.
Our rendering for boolean fields is determined here...
django-rest-framework/rest_framework/renderers.py
Lines 293 to 295 in c69e2e4
We probably need some explicit override in the
render_fieldmethod to switch thebase_templateforBooleanFieldwithnull=True?tbrknt commentedon Mar 10, 2021
I understand. So for
BooleanField, you suggest having two separate templates fornull=Trueandnull=False. What would you think of keeping only one template forBooleanFieldand change the input element based on thefield.allow_null? Would it be not a good idea to combine that logic in one template?Either way, would it be okay for you if I work on this issue?
lovelydinosaur commentedon Mar 11, 2021
I don't know. Possibly a bit odd for a template named
checkbox.htmlto sometimes render a checkbox, and sometimes render a select, but... 🤷♂️tbrknt commentedon Mar 11, 2021
I understand and I agree on that one (: So I will check the following multiple options to change this behavior:
BooleanFields are generated asChoiceFields if there is achoicesparameter on the model field definition. I was thinking to provide a defaultchoicestuple foryes,no, andunknown(as localized) ifallow_null=Trueon serializerBooleanFieldornull=Trueon modelBooleanFieldand if there is nochoicesprovided. That way, it can be generated as a ChoiceField and rendered asselect. However, there maybe some issues saving the data or loading the existing data. This is one option that I'm checking (:django-rest-framework/rest_framework/serializers.py
Lines 1215 to 1218 in f070619
choiceskwarg on the model field or serializer field if it is provided.Those are the options that I could come up with. Please let me know if I'm missing anything here. Thank you for your help @tomchristie .
lovelydinosaur commentedon Mar 12, 2021
Okay, so "I saw that model BooleanFields are generated as ChoiceFields if there is a choices parameter on the model field definition" doesn't actually help here, because that only applies in the case where
ModelSerializeris auto-generating a field. We also want things to work correctly when an explicitly listedBooleanField(allow_null=True)is included on the serializer class.pythonwood commentedon Mar 15, 2021
I had a little looked into drf source. because form/json data render and parser need to modify both, Maybe it take a lot patch ?
by the way, I wonder why drf donot reuse django.forms?
(WIP) Fix html form rendering of BooleanFields with allow_null=True e…
tbrknt commentedon Mar 26, 2021
Which part of the parser needs to be modified? I could not see it.
Here, I was working on the approach of overwriting the rendering template for boolean fields with allow_null=True. I have added some tests and I was planning to add more tests for nested fields. It would be nice to get some early-feedback. That was the desired behavior, right?

master...tbrknt:fix-null-boolean-rendering-7722
With the following model
TestBooleanand model serializer (TestBooleanSerializer), I was able to get the screenshot above.With this
Serializer,AnotherSerializer, I was able to see the HTML form on the second screenshot. What do you think?pythonwood commentedon Mar 27, 2021
Yeah, you do it. It is good enought for my case (It take "Unknow" became null is right, thanks).
Hope to good case in nest mode and hope to merge soon.
(WIP) Fix html form rendering of BooleanFields with allow_null=True e…
(WIP) Fix html form rendering of BooleanFields with allow_null=True e…
Fix encode#7722.
15 remaining items
merwok commentedon Nov 13, 2022
Should be fixed
auvipy commentedon Nov 22, 2022
can you try and review the related PR of this issue?
stale commentedon Oct 18, 2025
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
merwok commentedon Oct 19, 2025
I’ll have a look at the PR!
auvipy commentedon Nov 6, 2025
please do!
Danish-kh-IT commentedon Oct 22, 2026
Hi, I'd like to work on this.
From the discussion above, the agreed direction seems to be an explicit override in
HTMLFormRenderer.render_fieldso that aBooleanFieldwithallow_null=Truerenders as a select (Unknown/Yes/No) instead of a checkbox.I noticed PR #7882 attempted this but was closed without merging, and there are no linked branches or PRs anymore. @merwok — are you still planning to revive #7882? If not, I'm happy to pick this up with a fresh PR, starting with a failing test for the rendering.