How to use CommaSeperatedIntegerList with CheckboxSelectMultiple

1,142 views
Skip to first unread message

ben

unread,
Apr 2, 2010, 1:09:04 PM4/2/10
to Django users
Hi, I am new to Django. I am currently using the latest Django 1.2
from trunk. I am trying to store a list of values in a
CommaSeperatedIntegerList. I would like to use a
CheckboxSelectMultiple widget to render and select the values in the
list. I have setup my code like so.

SOME_CHOICES = (('1', 'ch1'),('2', 'ch2'), ('3', 'ch3'),('4',"ch4"),
('5',"ch5"),)

class QandA(models.Model):
q1 = models.CommaSeparatedIntegerField(choices=SOME_CHOICES,
max_length=100, null=True,blank=True)


class QandAForm(ModelForm):
class Meta:
model=QandA
fields=['q1']
widgets={'q1':forms.CheckboxSelectMultiple,}

The form renders properly but I get the following validation error
after posting.

"Select a valid choice. [u'1'] is not one of the available choices."

The same thing happens when I attempt to use SelectMultiple as the
widget. Can anyone tell me what I am doing wrong? I have googled about
but have not been able to find anything describing how to handle this.

Thanks, Ben

Bill Freeman

unread,
Apr 2, 2010, 1:50:03 PM4/2/10
to django...@googlegroups.com
I once saw something for this on djangosnippets. Search hard.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

ben

unread,
Apr 2, 2010, 2:38:10 PM4/2/10
to Django users
So what your saying is that I'm not using CommaSeperatedIntegerField
for its intended purpose? I should think about writing a set of custom
fields? I just assumed support for this type of thing was built in. I
will keep hunting.

orokusaki

unread,
Apr 2, 2010, 3:06:15 PM4/2/10
to Django users
The problem is that you're using '1' instead of 1. The comma
separated integer list is expecting integers not strings. The reason
you're seeing u'1' is because it's being turned from a string to a
unicode object.

Try this instead:

SOME_CHOICES = ((1, 'ch1'),(2, 'ch2'), (3, 'ch3'),(4, 'ch4'))

ben

unread,
Apr 2, 2010, 3:37:42 PM4/2/10
to Django users
Sorry. I pasted code that I was experimenting with. I thought maybe
the validation code was looking for strings because that is what the
error code said it was looking for. Before that I had been using
integers and that doesn't seem to work either. I am wondering if this
is a bug in Django 1.2. I hunted around on Django snippets as Bill
Freeman suggested and found the following code http://www.djangosnippets.org/snippets/1200/
for a custom multiple selection field.

I inserted it into my app and it doesn't want to validate the
selection either. In fact it returns a validation error message
similar to what I received from CommaSeperatedIntegerField and
CheckboxSelectMultiple. "Value [u'1'] is not a valid choice."

Not sure what is going on. Any insights would be appreciated.

Bill Freeman

unread,
Apr 2, 2010, 5:19:18 PM4/2/10
to django...@googlegroups.com
I know that I used (some revision of) that snippet a while back.

Realize that what it stores is a string, containing digits and commas.

The to_python and get_db_prep_value methods are responsible for
converting between that database single string and a list of strings,
not integers. You can use any string (that doesn't contain comma) to
represent a choice (db value). I had two character ID flags (easier to
read in pgadmin). It did work, but I forget the details (I eventually went
to multi to multi and the one end of many to one relationships). So I
expect that the DB side of your choice tuples must be strings.

Bill

ben

unread,
Apr 3, 2010, 11:30:42 AM4/3/10
to Django users
Either I don't understand how CheckboxSelectMultiple works or there is
a bug in Django 1.2. I have tried the following custom multi select
fields I have found around the web.

http://www.djangosnippets.org/snippets/1200/
http://www.davidcramer.net/code/181/custom-fields-in-django.html

Out of desperation I tried using a ManyToMany field and all produce
the same validation error when used with either CheckboxSelectMultiple
or SelectMultiple.

'Select a valid choice. [u'1', u'2', u'3', u'4'] is not one of the
available choices.'

This happens regardless of the number of choices, types of the choices
tuple values, or the model field type.

Can anyone point me to an example that demonstrates how to properly
use these fields.

Thanks,
Ben


On Apr 2, 5:19 pm, Bill Freeman <ke1g...@gmail.com> wrote:
> I know that I used (some revision of) that snippet a while back.
>
> Realize that what it stores is a string, containing digits and commas.
>
> The to_python and get_db_prep_value methods are responsible for
> converting between that database single string and a list of strings,
> not integers.  You can use any string (that doesn't contain comma) to
> represent a choice (db value).  I had two character ID flags (easier to
> read in pgadmin).  It did work, but I forget the details (I eventually went
> to multi to multi and the one end of many to one relationships).  So I
> expect that the DB side of your choice tuples must be strings.
>
> Bill
>
>
>
> On Fri, Apr 2, 2010 at 3:37 PM, ben <ben.k...@gmail.com> wrote:
> > Sorry. I pasted code that I was experimenting with. I thought maybe
> > the validation code was looking for strings because that is what the
> > error code said it was looking for. Before that I had been using
> > integers and that doesn't seem to work either. I am wondering if this
> > is a bug in Django 1.2. I hunted around on Django snippets as Bill

> > Freeman suggested and found the following codehttp://www.djangosnippets.org/snippets/1200/

Bill Freeman

unread,
Apr 4, 2010, 7:02:23 PM4/4/10
to django...@googlegroups.com
Perchance are you somehow passing a list of list? When the validator
strips off the outer list, it would get a list instead of a string, which
would match the error message (that's the only reason I suggest it).

Otherwise, pdb is your friend. I particularly like running the dev server
under emacs, because when you hit a breakpoint, emacs pops up the
relevant file in another "window" (what emacs calls panels). But then
I do everything in emacs, so your favorite tool may be just as good.

Bill

Keith

unread,
Apr 10, 2010, 12:53:31 AM4/10/10
to Django users

This sounds like a ChoiceField instead of a MultipleChoiceField.

A CheckboxSelectMultiple has to be used with a MultipleChoiceField.

Refer to:
http://ontehfritz.wordpress.com/2009/02/15/django-forms-choicefield-and-multiplechoicefield/

Hope this helps.

Kevin Renskers

unread,
Apr 26, 2010, 5:28:18 AM4/26/10
to Django users
I also used the multiple select field found on http://www.djangosnippets.org/snippets/1200/
with success, until I updated Django to the 1.2 beta release. It now
longer works, always giving the validation error. I have no clue how
to fix this, so I was hoping you found the answer to your problem and
can share it?

I have a tuple with (string, not integer) choices, and want to show
the user a multiple select field. The choices should be saved as a
comma separated string in the database.

Cheers,
Kevin

On Apr 3, 5:30 pm, ben <ben.k...@gmail.com> wrote:
> Either I don't understand how CheckboxSelectMultiple works or there is
> a bug in Django 1.2. I have tried the following custom multi select
> fields I have found around the web.
>
> http://www.djangosnippets.org/snippets/1200/http://www.davidcramer.net/code/181/custom-fields-in-django.html

Bill Freeman

unread,
Apr 26, 2010, 11:16:22 AM4/26/10
to django...@googlegroups.com
Kevin,

I've not used Djanog 1.2 yet, so I haven't seen and fixed any similar problem.

I can offer 2 suggestions:

1. pdb is your friend. Learn it, love it. (Very tasty under Gnu
Emacs with python mode,
start runserver from an emacs shell window with a pdb.set_trace()
style breakpoint
installed somewhere intresting, like just before you form.is_valid()
call and step on in.
But I'm sure that other development environments interface can also
use it well.)

2. Start a new thread and provide a lot more detail, including code
snippets and exact
validation error messages.

Bill

Preston Holmes

unread,
Apr 27, 2010, 12:46:57 AM4/27/10
to Django users


On Apr 26, 2:28 am, Kevin Renskers <i...@bolhoed.net> wrote:
> I also used the multiple select field found onhttp://www.djangosnippets.org/snippets/1200/
> with success, until I updated Django to the 1.2 beta release. It now
> longer works, always giving the validation error. I have no clue how
> to fix this, so I was hoping you found the answer to your problem and
> can share it?
>
> I have a tuple with (string, not integer) choices, and want to show
> the user a multiple select field. The choices should be saved as a
> comma separated string in the database.

The issue with the custom field is that it does not implement a
'validate' func

I believe all I did to get it working was add:

def validate(self, value, model_instance):
return


>
> Cheers,
> Kevin
>
> On Apr 3, 5:30 pm, ben <ben.k...@gmail.com> wrote:
>
>
>
>
>
> > Either I don't understand how CheckboxSelectMultiple works or there is
> > a bug in Django 1.2. I have tried the following custom multi select
> > fields I have found around the web.
>
> >http://www.djangosnippets.org/snippets/1200/http://www.davidcramer.ne...

Kevin Renskers

unread,
Apr 27, 2010, 4:51:06 AM4/27/10
to Django users
Works like a charm, thanks!

> The issue with the custom field is that it does not implement a
> 'validate' func
>
> I believe all I did to get it working was add:
>
>     def validate(self, value, model_instance):
>         return

Jannon Frank

unread,
May 29, 2010, 12:09:53 PM5/29/10
to Django users
Don't know if you're still interested, Ben,

But I just came across this as I was searching for the best place to
tell someone how the django CommaSeparatedIntegerField(CSIF) should be
changed.
I was basically trying to do the same thing you were.

After, taking a look at the django code, I found that CSIF subclasses
Field which performs some validation when a 'choices' parameter is
supplied. Unfortunately, that validation is not appropriate for what
you (and I) were trying to do with the CSIF -- have each integer
checked for membership in choices. Instead is just checks the whole
value (e.g. u"1,2,5") for membership.

So, to solve your immediate (well, two month old) problem, you could
either:
1. not set the choices parameter on the CSIF model field
2. write a subclass of CSIF and overwrite Field's validate() method to
do the right thing

In the long run, Field should really be modified so it's validation
can be overwritten at a more granular level (e.g.
validate_choices()). Then CSIF could just overwrite that bit.
-j

On Apr 3, 8:30 am, ben <ben.k...@gmail.com> wrote:
> Either I don't understand how CheckboxSelectMultiple works or there is
> a bug in Django 1.2. I have tried the following custom multi select
> fields I have found around the web.
>
> http://www.djangosnippets.org/snippets/1200/http://www.davidcramer.net/code/181/custom-fields-in-django.html
Reply all
Reply to author
Forward
0 new messages