Saturday, May 29, 2010

Re: How to use CommaSeperatedIntegerList with CheckboxSelectMultiple

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
>
> 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/
> > > 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.
>
> > > On Apr 2, 3:06 pm, orokusaki <flashdesign...@gmail.com> wrote:
> > >> 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'))
>
> > > --
> > > You received this message because you are subscribed to the Google Groups "Django users" group.
> > > To post to this group, send email to django-users@googlegroups.com.
> > > To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> > > For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

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

No comments:

Post a Comment