Skip to content

NullBooleanField returns boolean value False for empty QueryDict. #7582

Closed

Description

@cosmosgenius

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

Create a serializer as below with the test data

from django.http import QueryDict
from rest_framework import serializers

class TestSerializer(serializers.Serializer):
    is_active = serializers.NullBooleanField(required=False)

s_dict = TestSerializer(data={})
s_dict.is_valid()

s_qdict = TestSerializer(data=QueryDict())
s_qdict.is_valid()

Expected behavior

s_dict and s_qdict has the same value which is is_active: None

Actual behavior

s_dict returns is_active: None but s_qdict returns is_active: False

NullBooleanField was primarily used to avoid default_empty_html from BooleanField. Now that it is merged what is the alternative?

Activity

  1. amikrop commented on Oct 14, 2020

    @amikrop
    Contributor
  2. xordoquy commented on Oct 15, 2020

    @xordoquy
    Contributor

    This is likely because NullBooleanField doesn't override the BooleanField's default_empty_html value which is False.

    If you alter your field definition as is_active = serializers.NullBooleanField(required=False, default=None) you'll get a consistent behaviour.

    A PR with the appropriate test could fix that too.

  3. amikrop commented on Oct 15, 2020

    @amikrop
    Contributor

    That's right, are we sure though that the developers' intentions are for NullBooleanField's default_empty_html to be None?
    If yes, we can make that small PR, should be trivial.

  4. cosmosgenius commented on Oct 20, 2020

    @cosmosgenius
    Author

    That's right, are we sure though that the developers' intentions are for NullBooleanField's default_empty_html to be None?
    If yes, we can make that small PR, should be trivial.

    Yes that is the expectation. But @xordoquy alternate field definition works well with BooleanField which is the right way to do since NullBooleanField is deprecated.

    This does feel like a breaking change though in my opinion. If we didn't have proper CI/CD tests, this won't have been caught.

  5. beesnotincluded commented on Mar 22, 2021

    @beesnotincluded

    We too were caught out by this change. Our CI/CD test noticed the regression and we spent quite a while getting to the bottom of it.

  6. jaredmcdonald commented on Apr 21, 2021

    @jaredmcdonald

    Just ran across this when upgrading our project to to 3.12.x (from 3.11.x) and also only caught it because of tests.

    A BooleanField with default=None is still a bit of a departure from the previous behavior, where e.g. in @cosmosgenius' example above, is_active would not be present in validated_data if it wasn't passed in at all in the QueryDict. Is there some reason that the key should be present?

  7. Safrone commented on May 4, 2021

    @Safrone

    I faced this as well where I was expecting the empty query dict to not insert the boolean

    seeing that NullBooleanField is deprecated for DRF and django and I didn't actually need the third state (just using it to know whether true or false was explicitly given since default_empty_html otherwise obfuscates that), my solution is to override BooleanField:

    from rest_framework import fields, serializers
    
    class DefaultEmptyBooleanField(fields.BooleanField):
        default_empty_html = fields.empty
    
    class MySerializer(serializers.Serializer):
        boolean_field = DefaultEmptyBooleanField(required=False)
    

    use case:

    from django.http import QueryDict
    
    serializer = MySerializer(data=QueryDict(''))
    serializer.is_valid()
    serializer.validated_data # OrderedDict()
    
    serializer = MySerializer(data=QueryDict('boolean_field=true'))
    serializer.is_valid()
    serializer.validated_data # OrderedDict([('boolean_field', True)])
    

    You can add allow_null=True to DefaultEmptyBooleanField() for the added null option

  8. stale commented on Apr 30, 2022

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

  9. auvipy commented on Dec 4, 2022

    @auvipy
    Collaborator

    can any of you try #8614 and report back please? seems related to this issue

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions