Saturday, July 31, 2010

Re: How do I populate a multi-select field with a single column from a model?

Ah... Well that's exactly what's happening. I'm getting "Area Object"
for each element in the select box. I've seen the __unicode__ method
on a few model examples, but my book and other resources never really
showed or explained it. I'll see if I can find some explanation of it.
Thanks for the clue.

On Jul 31, 4:31 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Jul 31, 10:07 am, strayhand <tobyb...@gmail.com> wrote:
>
> > I want to grab a single column in a model and use it to populate a
> > multi-select form field. Here's the code that I'm currently using:
>
> > areas = forms.ModelMultipleChoiceField(queryset=Area.objects.all(),
> > label='Preferred Areas', help_text='Select the areas that you\'d like
> > to serve.')
>
> > This code returns the entire Model. I tried using
> > queryset=Area.values('name') and it didn't work. How am I suppose to
> > grab a single column out of a model? My model and form code have been
> > provided below. Thanks.
>
> I don't know what you mean by 'returns the entire Model'. Model choice
> fields use as their display values the __unicode__ value of the items
> in the queryset. If that isn't defined, you'll get something like
> "Area object". The solution is simply to define a __unicode__ method,
> which is good practice anyway.
> --
> DR.

--
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