models.BooleanField.blank should be 'False' if BooleanField has choices
|Reported by:||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
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
blankproperty, if a boolean field in a form can validate when in fact it is
None(OK, I'm assuming somewhere that BooleanField converts a
- 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
__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
False - but maybe the converting of
False should consider the widget type?