ModelForm vs CheckboxSelectMultiple

234 views
Skip to first unread message

Vance Dubberly

unread,
Sep 19, 2008, 11:58:46 PM9/19/08
to django...@googlegroups.com
So with a form that does this:
forms.ModelChoiceField(TransportMethod.objects.all(),
widget=forms.CheckboxSelectMultiple())

The first check box has no value and is labeled '---------' is there
an option to have that not exist. I don't don't see a passable param
which would turn it off in either ModelChoiceField or
CheckboxSelectMultiple. Am I blind?

Vance

--
To pretend, I actually do the thing: I have therefore only pretended to pretend.
- Jacques Derrida

Joshua Jonah

unread,
Sep 20, 2008, 12:00:35 AM9/20/08
to django...@googlegroups.com
You have to set a default.

Joshua Jonah

unread,
Sep 20, 2008, 12:03:17 AM9/20/08
to django...@googlegroups.com
... use the 'initial' attribute of the form field.

Vance Dubberly

unread,
Sep 20, 2008, 5:04:31 PM9/20/08
to django...@googlegroups.com
Unfortunately no luck

locations = forms.ModelChoiceField(Location.objects.all(),
widget=forms.CheckboxSelectMultiple(), initial='')
locations = forms.ModelChoiceField(Location.objects.all(),
widget=forms.CheckboxSelectMultiple(), initial=None)

no impact.

Vance

--

Vance Dubberly

unread,
Sep 20, 2008, 6:26:48 PM9/20/08
to django...@googlegroups.com
End result is I duplicated the code form the CheckboxSelectMultiple
and made my own. Only difference is that I test for for an option
value and don't append to the form if there isn't one

if option_value:
...
output.append(u'<li><label%s>%s %s</label></li>' % (label_for,
rendered_cb, option_label))

Vance

Martin Ostrovsky

unread,
Sep 20, 2008, 7:42:32 PM9/20/08
to Django users
When creating your widget, you need to set empty_label = None, this
will supress the '----' choice.

Also, I think you want to use forms.ModelMultipleChoiceField in
conjunction with the widget forms.CheckboxSelectMultiple.

On Sep 20, 6:26 pm, "Vance Dubberly" <altjeri...@gmail.com> wrote:
> End result is I duplicated the code form the CheckboxSelectMultiple
> and made my own.  Only difference is that I test for for an option
> value and don't append to the form if there isn't one
>
> if option_value:
>    ...
>     output.append(u'<li><label%s>%s %s</label></li>' % (label_for,
> rendered_cb, option_label))
>
> Vance
>
>
>
> On Sat, Sep 20, 2008 at 2:04 PM, Vance Dubberly <altjeri...@gmail.com> wrote:
> > Unfortunately no luck
>
> > locations = forms.ModelChoiceField(Location.objects.all(),
> > widget=forms.CheckboxSelectMultiple(), initial='')
> > locations = forms.ModelChoiceField(Location.objects.all(),
> > widget=forms.CheckboxSelectMultiple(), initial=None)
>
> > no impact.
>
> > Vance
>

Vance Dubberly

unread,
Sep 20, 2008, 11:09:27 PM9/20/08
to django...@googlegroups.com
Martin,
Thank you, that let me delete my extra bunch of code and made me
feel silly for even going there. ;)

Vance

Reply all
Reply to author
Forward
0 new messages