models.BooleanField.blank should be 'False' if BooleanField has choices
|Reported by:||django@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.6|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
There's no way to override the 'blank' property on a boolean field.
If you look at db/models/fields/init.py under BooleanField.__init__() you'll see that blank = True is hard coded.
This gets messy for a couple of reasons:
- It means that doing something like this silently fails. Either it should raise an error (e.g. "blank cannot be set for BooleanField") or in __init__ do a check to only set blank=True if kwargs.get('blank') is None
class MyObj(models.Model): is_good = models.BooleanField(blank=False) # fails silently since blank = True is hard coded
- It makes me question the strictness of the blank property, if a boolean field in a form can validate when in fact it is False and not None (OK, I'm assuming somewhere that BooleanField converts a None value to False?
- Most importantly (and the case where this broke for me) is when I wanted to do something like:
class MyObj(models.Model): is_good = models.BooleanField(choices=( (False, "Object is Bad"), (True, "Object is Good") )
There's no way to make sure the value is not blank.
My simple fix was to edit BooleanField's __init__ method to do:
def __init__(self, *args, **kwargs): if not kwargs.get('choices'): kwargs['blank'] = True Field.__init__(self, *args, **kwargs)
But now I think about it maybe the fact that BooleanField assumes a None value to be False is another problem as well. I know why - because the default widget is a tickbox, which have the same state for None and False - but maybe the converting of None to False should consider the widget type?