Skip to content

NullBooleanField should use forms.NullBooleanField just like django. And BooleanField should support default=True #7722

Open

Description

@pythonwood

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • 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)),
]

run shell command

./manage.py makemigrations boolean
./manage migrate
./manage runserver

stdout

......
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.
......

Expected behavior

django doc https://docs.djangoproject.com/en/3.1/ref/models/fields/#booleanfield
class BooleanField(**options)¶
A true/false field.

The default form widget for this field is CheckboxInput, or NullBooleanSelect if null=True.

The default value of BooleanField is None when Field.default isn’t defined.

image

Actual behavior

django rest framework doc

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.

image
image

Activity

  1. 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
  2. tbrknt commented on Mar 9, 2021

    @tbrknt
    Contributor

    Hello,

    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.

    That is the representation of the serializer:
    TestBooleanSerializer():
        id = IntegerField(label='ID', read_only=True)
        acknowledged = BooleanField(allow_null=True, required=False)
        autoRenewing = BooleanField(allow_null=True, label='AutoRenewing', required=False)

    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:

    class TestBoolean(models.Model):
        CHECKLIST_OPTIONS = (
            (None, 'Unknown'),
            (True, 'Yes'),
            (False, 'No'),
        )
    
        acknowledged = models.BooleanField(null=True, blank=True)
        autoRenewing = models.NullBooleanField()
    
    class TestBooleanSerializer(serializers.ModelSerializer):
        autoRenewing = serializers.NullBooleanField()
        class Meta:
            model = TestBoolean
            fields = '__all__'

    BrowsableAPI with dropdown for Boolean Field

    So do you think that this should be the default behavior when null=True for a boolean field is provided?

  3. lovelydinosaur commented on Mar 10, 2021

    @lovelydinosaur
    Member

    So @pythonwood is referring to django.forms.NullBooleanField, not django.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.

  4. lovelydinosaur commented on Mar 10, 2021

    @lovelydinosaur
    Member

    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...

    serializers.BooleanField: {
    'base_template': 'checkbox.html'
    },

    We probably need some explicit override in the render_field method to switch the base_template for BooleanField with null=True ?

  5. tbrknt commented on Mar 10, 2021

    @tbrknt
    Contributor

    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?

  6. lovelydinosaur commented on Mar 11, 2021

    @lovelydinosaur
    Member

    I don't know. Possibly a bit odd for a template named checkbox.html to sometimes render a checkbox, and sometimes render a select, but... 🤷‍♂️

  7. tbrknt commented on Mar 11, 2021

    @tbrknt
    Contributor

    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 (:
      if 'choices' in field_kwargs:
      # 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 .

  8. lovelydinosaur commented on Mar 12, 2021

    @lovelydinosaur
    Member

    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.

  9. pythonwood commented on Mar 15, 2021

    @pythonwood
    Author

    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?

  10. tbrknt commented on Mar 26, 2021

    @tbrknt
    Contributor

    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
    image

    With the following model TestBoolean and model serializer (TestBooleanSerializer), I was able to get the screenshot above.

    class TestBoolean(models.Model):
        CHECKLIST_OPTIONS = (
            (None, 'Unknown'),
            (True, 'Yes'),
            (False, 'No'),
        )
    
        boolean_choices_field = models.BooleanField(null=True, choices=CHECKLIST_OPTIONS)
        nullable_boolean_field_1 = models.BooleanField(null=True)
        nullable_boolean_field_2 = models.BooleanField(null=True)
        nullable_boolean_field_3 = models.BooleanField(null=True)
        null_boolean_field_1 = models.NullBooleanField()
        null_boolean_field_2 = models.NullBooleanField()
        null_boolean_field_3 = models.NullBooleanField()
        regular_boolean_field = models.BooleanField()
    
    
    class TestBooleanSerializer(serializers.ModelSerializer):
        class Meta:
            model = TestBoolean
            fields = '__all__'

    image

    class AnotherSerializer(serializers.Serializer):
        nullable_boolean_field_1 = serializers.BooleanField(allow_null=True)
        nullable_boolean_field_2 = serializers.BooleanField(allow_null=True)
        nullable_boolean_field_3 = serializers.BooleanField(allow_null=True)
        null_boolean_field_1 = serializers.NullBooleanField()
        null_boolean_field_2 = serializers.NullBooleanField()
        null_boolean_field_3 = serializers.NullBooleanField()
        regular_boolean_field = serializers.BooleanField()

    With this Serializer, AnotherSerializer, I was able to see the HTML form on the second screenshot. What do you think?

  11. pythonwood commented on Mar 27, 2021

    @pythonwood
    Author

    有了这个Serializer,AnotherSerializer我能看到的第二个屏幕的HTML表单。你怎么认为?

    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.

  12. 15 remaining items

  13. merwok commented on Nov 13, 2022

    @merwok
    Contributor

    Should be fixed

  14. auvipy commented on Nov 22, 2022

    @auvipy
    Collaborator

    Should be fixed

    can you try and review the related PR of this issue?

  15. added this to the 3.14 milestone on Nov 22, 2022
  16. removed this from the 3.15 milestone on Jul 11, 2023
  17. stale commented on Oct 18, 2025

    @stale

    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.

  18. merwok commented on Oct 19, 2025

    @merwok
    Contributor

    I’ll have a look at the PR!

  19. auvipy commented on Nov 6, 2025

    @auvipy
    Collaborator

    please do!

  20. Danish-kh-IT commented on Oct 22, 2026

    @Danish-kh-IT

    Hi, I'd like to work on this.

    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.

  21. modified the milestones: 3.18, 3.19 on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions