Saturday, July 31, 2010

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

Sweet. That did the trick. I found an example here:

http://www.djangoproject.com/documentation/models/str/

# Areas Model UPDATED
from django.db import models

# Create your models here.
class Area(models.Model):
name = models.CharField(max_length=40)
city = models.CharField(max_length=50)
phone = models.CharField(max_length=12)
email = models.EmailField()

def __unicode__(self):
return self.name

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