-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
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.
Actual behavior
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.