This has been discussed many times in the past. For better or worse, we're stuck with the current behaviour for backwards compatibility.
I personally think it's better this way. Without this behaviour, it would be a PITA to implement forms that have a "required" boolean field (must be ticked) without repeating yourself constantly by writing custom validation for those fields. Most forms I work with have a "Yes, I have agreed to the terms and conditions" or "Yes, I want to receive emails from XYZ" type fields. If your boolean fields are not "required" (as per the definition in Django forms, "must be ticked"), why can't you just put `required=False` in your form? I'd try to avoid patching your local Django with a change like this unless absolutely necessary so that you can cleanly upgrade and don't end up writing code that does the opposite of what everyone else expects. Cheers. Tai. On Jun 17, 5:14 am, Michael Blume <[email protected]> wrote: > In Django BooleanFields, the required flag is used to mean that the field > must be checked for the form to validate. Required is True by default for > all Fields, so this is the default behavior. > > I strongly suspect that this violates principle of least surprise for most > people including Boolean Fields in forms. It did for me. > > I've patched it in my local Django checkout. I realize this is a > backwards-incompatible change, so it might not be eligible for trunk any > time soon, but FWIW, here's the patch: > > --- i/django/forms/fields.py > +++ w/django/forms/fields.py > @@ -606,6 +606,11 @@ class URLField(CharField): > class BooleanField(Field): > widget = CheckboxInput > > + def __init__(self, *args, **kwargs): > + if not args and 'required' not in kwargs: > + kwargs['required'] = False > + return super(BooleanField, self).__init__(*args, **kwargs) > + > def to_python(self, value): > """Returns a Python boolean object.""" > # Explicitly check for the string 'False', which is what a hidden > field -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
