Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#30216 closed Cleanup/optimization (fixed)

Document BooleanField is no longer blank=True in Django 2.1+

Reported by: Ed Morley Owned by: David Vaz
Component: Documentation Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hi!

In Django 2.1, as part of adding null support to BooleanField, the previously implicit/hardcoded blank=True of BooleanField was removed:
https://github.com/django/django/commit/5fa4f40f45fcdbb7e48489ed3039a314b5c961d0#diff-bf776a3b8e5dbfac2432015825ef8afeL995

In one of our projects, we had a model with a BooleanField defined like so:

class PerformanceAlert(models.Model):
    id = models.AutoField(primary_key=True)
    # ...
    is_regression = models.BooleanField()

The REST API for that model uses django-rest-framework's ModelSerializer, which generates validation rules based on the model properties.

In Django 2.0, the d-r-f API serializer's repr() is:

PerformanceAlertSerializer():
    id = IntegerField(read_only=True)
    # ...
    is_regression = BooleanField(required=False)

But under Django 2.1 this changed to

PerformanceAlertSerializer():
    id = IntegerField(read_only=True)
    # ...
    is_regression = BooleanField()

As such under Django 2.1 API calls that were previously successful then failed with This field is required.. (It turned out our API is using PUTs in places it should really be using PATCH.)

We were able to resolve the issue by adjusting PerformanceAlertSerializer such that it explicitly configures the field with serializers.BooleanField(required=False), however it would be great to mention this ~breaking change in the Django 2.1 release notes (and also as a "changed in" on the BooleanField entry on the fields page) - particularly since it looks like several other people have hit the same issue:
https://github.com/django/django/commit/5fa4f40f45fcdbb7e48489ed3039a314b5c961d0#r30206260
https://code.djangoproject.com/ticket/29921

If I get a chance I'll open a PR in the next few weeks, but happy for someone to beat me to it.

Many thanks :-)

Change History (5)

comment:1 by Tim Graham, 6 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

I'm not sure what the note would say exactly. Is "BooleanField.__init__() no longer sets blank=True." enough? It doesn't seem to give much explanation about possible ramifications of the change and it's not Django's place to describe how django-rest-framework is affected. This behavior isn't documented in ref/models/fields.txt, and we normally don't include a versionchanged annotation for something that's undocumented.

comment:2 by David Vaz, 5 years ago

Owner: changed from nobody to David Vaz
Status: newassigned
Last edited 5 years ago by Mariusz Felisiak (previous) (diff)

comment:3 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In a6972e88:

[2.2.x] Fixed #30216 -- Doc'd that BooleanField is no longer blank=True in Django 2.1.

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 1556a67c:

[2.1.x] Fixed #30216 -- Doc'd that BooleanField is no longer blank=True in Django 2.1.

Backport of a6972e88547ad5a51592f2b6d5046754c4b59394 from stable/2.2.x

comment:5 by Mariusz Felisiak, 5 years ago

Has patch: set
Note: See TracTickets for help on using tickets.
Back to Top