leonardopessoa leonardopessoa - 1 year ago 70
Python Question

Django (1.9) makemigrations doesn't get Blank=True on boolean field

I have the following model:

class Site(models.Model):
Model for a site entry
@author: Leonardo Pessoa
@since: 05/09/2016
from decimal import Decimal

consolidated_financials = models.BooleanField(blank=True)
type = models.ForeignKey(Type)
tier1_business = models.ForeignKey(Business, limit_choices_to = {'tier': 1}, related_name='%(class)s_tier1')

Note that the
field has now the
This was newly included. When I ran makemigrations, it didn't get the change, but when I add to
it worked normally.

Is there a restriction with the Boolean field specifically?


Answer Source

BooleanField does not take null=True, instead, use NullBooleanField.

There is a great answer on Programmers.se by Jacob Maristany that explains the reasoning behind this well

By allowing nulls in a boolean field, you are turning an intended binary representation (true/false) into a tri-state representation (true, false, null) where your 'null' entries are indeterminate.

For the full discussion, see Should I store False as Null in a boolean database field?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download