Using CheckboxSelectMultiple

194 views
Skip to first unread message

Alex Ezell

unread,
Dec 17, 2007, 11:14:58 PM12/17/07
to django...@googlegroups.com
I have several fields define in my models like this:

CREDENTIALS_CHOICES = (
        ('LM', "Commercial Driver's License"),
        ('IN', 'Insured'),
        ('DR', 'DOT Registered (Interstate)'),
        ('FI', 'Fragile Item Qualified'),
)
credentials = models.CharField(max_length=2, blank=True, choices=CREDENTIALS_CHOICES)

Then, I have them described in my form class as this:

credentials = forms.ChoiceField(choices=Truckshare.CREDENTIALS_CHOICES)

What I would like to do is use a CheckboxSelectMultiple for this field. However, when I do that like this:

credentials = forms.ChoiceField(choices=Truckshare.CREDENTIALS_CHOICES,widget=CheckboxSelectMultiple)

but that will fail because it will submit multiple values to the model and since it's ChoiceField, it will complain.

How can I set this up so that I can use choices in my model and use a CheckboxSelectMultiple field via newforms?

Thanks!

/alex


Tim

unread,
Dec 18, 2007, 11:46:20 AM12/18/07
to Django users
Try this:

credentials =
forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,
choices=Truckshare.CREDENTIALS_CHOICES)

Alex Ezell

unread,
Dec 23, 2007, 1:20:13 PM12/23/07
to django...@googlegroups.com
Tim,
Sorry for the late reply, but I'm just now getting around to this.

This definitely gives me the widget I want and the Forms class is happy, but I can't seem to find anything about what type of DB field or Model field this should be.

The errors I get say that the value posted is of type text[], but I'm not sure how to cast that or whatever to get the database to accept and then of course be able to edit it or display it.

Thanks!

/alex

On Dec 18, 2007 10:46 AM, Tim < tims...@gmail.com> wrote:

Try this:

credentials =
forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple ,

Alex Ezell

unread,
Dec 23, 2007, 1:52:21 PM12/23/07
to django...@googlegroups.com
OK, I figured out the array type in Postgresql to get this to work.

Now, the challenge is to read through the array when I am displaying the values and match them up with the choices in the model.

/alex

lispingng

unread,
Dec 24, 2007, 3:17:06 PM12/24/07
to Django users
Hi Alex,
I read through your post but am not very clear what you are trying to
achieve.
you have these choices and you want users to select multiple options,
right?
now for your model/database, do you plan to store all these options in
the same
credentials field, or you'll have another table hold the credentials
info and just use a
foreignKey to link to it? This, I think is my preference. to keep the
design simple

On Dec 23, 7:52 pm, "Alex Ezell" <aez...@gmail.com> wrote:
> OK, I figured out the array type in Postgresql to get this to work.
> Now, the challenge is to read through the array when I am displaying the
> values and match them up with the choices in the model.
>
> /alex
>
> On Dec 23, 2007 12:20 PM, Alex Ezell <aez...@gmail.com> wrote:
>
> > Tim,Sorry for the late reply, but I'm just now getting around to this.
>
> > This definitely gives me the widget I want and the Forms class is happy,
> > but I can't seem to find anything about what type of DB field or Model field
> > this should be.
>
> > The errors I get say that the value posted is of type text[], but I'm not
> > sure how to cast that or whatever to get the database to accept and then of
> > course be able to edit it or display it.
>
> > Thanks!
>
> > /alex
>

Tim

unread,
Dec 28, 2007, 12:58:09 PM12/28/07
to Django users
You'll need to use getlist('credentials') to access the form data (use
this in your validation code or in form processing) or:

for cred in data['credentials']:
newthing.credentials.add(cred)
newthing.save()

I think you're looking for the getlist() stuff.

Tim
Reply all
Reply to author
Forward
0 new messages