Re: Make "required=True" on forms.fields.NullBooleanField do something useful?
Sorry this is quite late on this thread, but I hadn't noticed any answer for it. How about: from django import forms class MyNullBooleanField(forms.NullBooleanField): def clean(self, value): if value is None: raise forms.ValidationError('Choose either Yes or No.') return super(MyNullBooleanField, self).clean(value) class YourForm(forms.Form): whatever_field_name = MyNullBooleanField() You could also subclass the django.db.models.fields.NullBooleanField and overload the formfield method if you want this for free on a ModelForm, but I suspect we're heading into django-users territory already. Cheers, Gary On Thu, Jun 17, 2010 at 10:39 AM, Matt Hoskins wrote: > My use case is that I want to have a BooleanField on the model and on > the form want to have the choices of "Unknown", "Yes" and "No" in a > select list and give a "this field is required" error if the user > leaves it set to "Unknown" (i.e. require them to actively choose "Yes" > or "No", rather than defaulting to either of them). I thought I'd just > be able to use NullBooleanField with "required=True" set, but > NullBooleanField just ignores the "required" argument. To my mind it > would be a sensible use of "required" for NullBooleanField, but before > raising a ticket I thought I'd ask if it was a deliberate oversight or > if what I'm suggesting isn't sensible for some reason :). > > Looking at the code for forms.fields.BooleanField and > forms.fields.NullBooleanField I guess the change would be in to_python > in the latter to check in the case where it would return None if > self.required is true and raise the validation error if that's the > case (i.e. similar to what BooleanField.to_python does for values of > False). > > Matt > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To post to this group, send email to django-develop...@googlegroups.com. > To unsubscribe from this group, send email to > django-developers+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: Make "required=True" on forms.fields.NullBooleanField do something useful?
> > Looking at the code for forms.fields.BooleanField and > forms.fields.NullBooleanField I guess the change would be in to_python > in the latter to check in the case where it would return None if > self.required is true and raise the validation error if that's the > case (i.e. similar to what BooleanField.to_python does for values of > False). As an aside, I'm now wondering if it's actually incorrect in Django 1.2 that BooleanField.to_python is what does the self.required check - shouldn't it be done in a method called validate? The documentation for the field api for 1.2 says "the to_python() method on a Field is the first step in every validation, it coerces the value to correct datatype and raises ValidationError if that is not possible" - I'd argue that False is of the correct data type (i.e. just because required is set to True doesn't mean that False now is not of the correct data type for BooleanField) so to_python shouldn't be doing this check. I think it's probably an oversight as the code that's now in "to_python" on BooleanField in 1.2 is what was in "clean" in 1.1, so it was probably just moved wholesale. Matt -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: Make "required=True" on forms.fields.NullBooleanField do something useful?
Because "required=True" on a BooleanField means "raise a required validation error if the value is anything other than True" so although using that combination that would give me the drop down list, it wouldn't give me the behaviour I desire which is to only raise the validation error if None is selected. I could handle it on the form, it just seems to me that given "required=True" is ignored on NullBooleanField it would be a sensible (and logical) use for it to have it mean "require the user to pick either Yes or No". (George - in case this is where you're not clear - in my use case I don't want the user to be able to just hit the submit button without having actively selected either "Yes" or "No" for these fields - if the choices were just "Yes" or "No" then one of those would have to be the default and thus the default value would be valid so the user could just hit submit without interacting with the field, whereas if the selection list includes "Unknown" and defaults to that, the user has to interact with the field and change it to "Yes" or "No" to pass validation). On Jun 17, 12:07 pm, Hanne Moa wrote: > On 17 June 2010 13:01, Matt Hoskins wrote: > > > For the reasons I gave in my explanation above - which bit isn't > > clear? > > Why not reuse the NullBoolean widget with a Boolean field? > > HM -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: Make "required=True" on forms.fields.NullBooleanField do something useful?
On 17 June 2010 13:01, Matt Hoskins wrote: > For the reasons I gave in my explanation above - which bit isn't > clear? Why not reuse the NullBoolean widget with a Boolean field? HM -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: Make "required=True" on forms.fields.NullBooleanField do something useful?
For the reasons I gave in my explanation above - which bit isn't clear? > If you require a "Yes" or "No" choice, why do you use a > NullBooleanField in the first place and not a BooleanField ? > > George -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Re: Make "required=True" on forms.fields.NullBooleanField do something useful?
On Jun 17, 11:39 am, Matt Hoskins wrote: > My use case is that I want to have a BooleanField on the model and on > the form want to have the choices of "Unknown", "Yes" and "No" in a > select list and give a "this field is required" error if the user > leaves it set to "Unknown" (i.e. require them to actively choose "Yes" > or "No", rather than defaulting to either of them). I thought I'd just > be able to use NullBooleanField with "required=True" set, but > NullBooleanField just ignores the "required" argument. To my mind it > would be a sensible use of "required" for NullBooleanField, but before > raising a ticket I thought I'd ask if it was a deliberate oversight or > if what I'm suggesting isn't sensible for some reason :). If you require a "Yes" or "No" choice, why do you use a NullBooleanField in the first place and not a BooleanField ? George -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
Make "required=True" on forms.fields.NullBooleanField do something useful?
My use case is that I want to have a BooleanField on the model and on the form want to have the choices of "Unknown", "Yes" and "No" in a select list and give a "this field is required" error if the user leaves it set to "Unknown" (i.e. require them to actively choose "Yes" or "No", rather than defaulting to either of them). I thought I'd just be able to use NullBooleanField with "required=True" set, but NullBooleanField just ignores the "required" argument. To my mind it would be a sensible use of "required" for NullBooleanField, but before raising a ticket I thought I'd ask if it was a deliberate oversight or if what I'm suggesting isn't sensible for some reason :). Looking at the code for forms.fields.BooleanField and forms.fields.NullBooleanField I guess the change would be in to_python in the latter to check in the case where it would return None if self.required is true and raise the validation error if that's the case (i.e. similar to what BooleanField.to_python does for values of False). Matt -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.