Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta
#7,722 23 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
38/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
django, python
Ambito
api, backend

Direzione di ricerca

Inizia con la riproduzione booleana minima in boolean/models.py e con il relativo TestBooleanSerializer, quindi esegui i comandi di migrazione e del server Django elencati per osservare il comportamento di BooleanField e NullBooleanField. Il lavoro è completato quando il comportamento del serializer corrisponde alla gestione prevista dei campi dei form Django e conserva un default=True esplicito durante l'input di un form HTML, con la copertura dei casi segnalati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Python
Stelle
30.2k
Fork
7.1k
Merge medio
1g 20h
PR unite (30g)
19

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di encode/django-rest-framework

Tutte le issue di encode/django-rest-framework

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.