Re: [Django] #22282: models.BooleanField.blank should be 'False' if BooleanField has choices

2014-03-20 Thread Django
#22282: models.BooleanField.blank should be 'False' if BooleanField has choices
-+-
 Reporter:  django@… |Owner:  nobody
 Type:  Uncategorized|   Status:  closed
Component:  Database layer   |  Version:  1.6
  (models, ORM)  |   Resolution:  needsinfo
 Severity:  Normal   | Triage Stage:
 Keywords:   |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  1|UI/UX:  0
-+-
Changes (by aaugustin):

 * status:  new => closed
 * needs_better_patch:   => 0
 * resolution:   => needsinfo
 * needs_tests:   => 0
 * needs_docs:   => 0


Comment:

 Let's start with form fields. A `BooleanField` is rendered by default as a
 checkbox. If it's mandatory (e.g. accept terms & conditions), you set
 `blank = False`. If it's optional, you set `blank = True`.

 Let's now look at model fields. Only the second case makes sense. That's
 why Django forces `blank = True`. You're right, Django could warn that
 `blank = False` is a misconfiguration that probably doesn't do what you
 expect.

 If I understand correctly, you'd like to use a different widget, maybe a
 radio list with two choices. If a value that doesn't match either of your
 choices is returned, as far as I can tell, `BooleanField.to_python` is
 going to raise a `ValidationError`.

 I don't understand why you're saying that "There's no way to make sure the
 value is not blank." On the contrary, Django enforces that you always get
 `True`, `False` or an exception at the model layer. As explained above,
 always getting `True` or an execption isn't interesting at the model
 layer.

 I'm not sure where "`BooleanField` assumes a `None` value to be `False`"
 comes from either.

 Could you show why your example doesn't work? Either an exception
 traceback or a failing test case will do. Thank you!

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/078.d95cfa6cfa77cd4678ef0dba19411389%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


[Django] #22282: models.BooleanField.blank should be 'False' if BooleanField has choices

2014-03-17 Thread Django
#22282: models.BooleanField.blank should be 'False' if BooleanField has choices
--+
 Reporter:  django@…  |  Owner:  nobody
 Type:  Uncategorized | Status:  new
Component:  Database layer (models, ORM)  |Version:  1.6
 Severity:  Normal|   Keywords:
 Triage Stage:  Unreviewed|  Has patch:  0
Easy pickings:  1 |  UI/UX:  0
--+
 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?

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.34fc5a36ea330ece6654100ef4e7ade3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.