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
> --
> 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.
>
>
Try this instead:
SOME_CHOICES = ((1, 'ch1'),(2, 'ch2'), (3, 'ch3'),(4, 'ch4'))
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.
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
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/
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
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.