Saturday, July 31, 2010

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

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