2

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 consolidated_financials field has now the blank=True statement. This was newly included. When I ran makemigrations, it didn't get the change, but when I add to finance_manager_sso it worked normally.

Is there a restriction with the Boolean field specifically?

Thanks

Leonardo Pessoa
  • 248
  • 4
  • 16
  • Related: [differentiate null=True, blank=True in django](http://stackoverflow.com/q/8609192/1324033) – Sayse Jul 07 '16 at 20:20

2 Answers2

3

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?

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 2
    Just to add to this, OP may be looking for `null=True`, if you're trying to allow a 'checked', 'unchecked', or 'unspecified' value. – Spencer Judd Jul 07 '16 at 20:21
  • 2
    BooleanField does not take null=True, instead, use NullBooleanField. Thanks for the help! – Leonardo Pessoa Jul 07 '16 at 20:29
  • Huh, that's interesting. Good find! Anyone know the reason why a `BooleanField` couldn't/shouldn't work with `null=True`? Besides, of course, that the documentation says so. :) – Spencer Judd Jul 07 '16 at 20:33
  • 2
    Any change to the serialized field will create a migration, even if that change doesn't affect the database. This allows use of the field options at a specific point in the migration plan by e.g. a `RunPython` operation. You can easily test this with the `blank` parameter on e.g. an `IntegerField`, as the OP noticed as well. – knbk Jul 07 '16 at 21:26
  • @knbk - Yes I realised this myself too after answering (been a long day) although Im not able to delete my answer anymore, upvoted yours anyway – Sayse Jul 07 '16 at 21:28
  • @knbk - I've now edited that out of my answer completely. – Sayse Jul 08 '16 at 06:43
3

The blank parameter is not used by BooleanField. Instead, it is hard-coded to True. Passing blank=False has no effect, so the migration autodetector doesn't detect any changes to the field, and doesn't create any migrations.

Since the blank parameter is used by IntegerField, passing in blank=False will lead to a change in the serialized field. The migration autodetector will detect that change and create a migration (even though that change doesn't affect the database).

knbk
  • 52,111
  • 9
  • 124
  • 122