#31971: Would like a BooleanChoiceField in Forms
-------------------------------------+-------------------------------------
     Reporter:  Terence Collins      |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Forms                |                  Version:  3.1
     Severity:  Normal               |               Resolution:
     Keywords:  boolean widget       |             Triage Stage:
  radioselect radio BooleanField     |  Unreviewed
  ChoiceField forms                  |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Terence Collins:

Old description:

> I am using a ChoiceField with a RadioSelect to provide an input to a
> boolean field, however, the value in forms.cleaned_data comes through as
> a string.  I understand why a choice field would result in a string,
> however if I place booleans in my values I'd expect them to pass simple
> equality tests against booleans in the model.   Other users implementing
> this model will likely come up against a bug where False ≠ 'False'.   So
> I'd request a widget (BooleanChoiceField) to cover this case: often a
> simple checkbox will not provide sufficient interface guidance to a user
> on the implications of their true/false options.
>
> {{{#!python
> #in forms.py
> class TimingForm(forms.ModelForm):
>
>     TIMING_LEVEL_CHOICES = ( (False, "Time Whole Lines (recommended)"),
>                               (True, "Time Individual Words"), )
>
>     word_level_timing = forms.ChoiceField(label='Timing Level',
> required=True, widget=forms.RadioSelect, choices=TIMING_LEVEL_CHOICES,
> initial=False)
>     class Meta:
>         model = TimingThingie
>         fields = ['word_level_timing']
>
> #in models.py
> class TimingThingie(models.Model):
>     word_level_timing = models.BooleanField(default=False)
>
> #in views.py:
> def modify_timing(request, tinstance_id):
>     tinstance = TimingThingie.objects.get(id=tinstance_id)
>     if tinstance.word_level_timing !=
> boolform.cleaned_data["word_level_timing"]:
>         #one is False, the other is 'False', inequality is true against
> intent and likely introduces a bug
>         pass
>     if tinstance.word_level_timing !=
> bool(strtobool(form.cleaned_data["word_level_timing"])):
>         #this works correctly, but is ugly and born only through
> suffering
>         pass
>   }}}

New description:

 I am using a ChoiceField with a RadioSelect to provide an input to a
 boolean field, however, the value in forms.cleaned_data comes through as a
 string.  I understand why a choice field would result in a string, however
 if I place booleans in my values I'd expect them to pass simple equality
 tests against booleans in the model.   Other users implementing this model
 will likely come up against a bug where False ≠ 'False'.   So I'd request
 a widget (BooleanChoiceField) to cover this case: often a simple checkbox
 will not provide sufficient interface guidance to a user on the
 implications of their true/false options.

 {{{#!python
 #in forms.py
 class TimingForm(forms.ModelForm):

     TIMING_LEVEL_CHOICES = ( (False, "Time Whole Lines (recommended)"),
                               (True, "Time Individual Words"), )

     word_level_timing = forms.ChoiceField(label='Timing Level',
 required=True, widget=forms.RadioSelect, choices=TIMING_LEVEL_CHOICES,
 initial=False)
     class Meta:
         model = TimingThingie
         fields = ['word_level_timing']

 #in models.py
 class TimingThingie(models.Model):
     word_level_timing = models.BooleanField(default=False)

 #in views.py:
 def modify_timing(request, tinstance_id):
     tinstance = TimingThingie.objects.get(id=tinstance_id)
     if tinstance.word_level_timing !=
 form.cleaned_data["word_level_timing"]:
         #one is False, the other is 'False', inequality is true against
 intent and likely introduces a bug
         pass
     if tinstance.word_level_timing !=
 bool(strtobool(form.cleaned_data["word_level_timing"])):
         #this works correctly, but is ugly and born only through suffering
         pass
   }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31971#comment:1>
Django <https://code.djangoproject.com/>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.032c109a5f8a6d7025ae7b4c5fdaf648%40djangoproject.com.

Reply via email to