Saturday, July 31, 2010

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

Area.objects.values('name','city')

On Jul 31, 5:07 pm, 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.
>
> #register.forms
>
> from django import forms
> from django.db import models
> from django.forms import ModelForm
> from ylbbq.areas.models import Area
>
> STATES = (
>         ('AK', 'Alaska'),
> ...
>         ('YT', 'Yukon'),
> )
>
> COUNTRIES = (
>         ('USA', 'United States'),
>         ('Canada', 'Canada')
> )
>
> class RegisterForm(forms.Form):
>         username = forms.CharField(max_length=30)
>         first_name = forms.CharField(max_length=30)
>         last_name = forms.CharField(max_length=30)
>         password1 = forms.CharField(max_length=60)
>         password2 = forms.CharField(max_length=60)
>         email = forms.EmailField(help_text='Enter a valid e-mail address.')
>         phone = forms.CharField(max_length=12)
>         address = forms.CharField(max_length=70)
>         city = forms.CharField(max_length=50)
>         state_province = forms.ChoiceField(choices=STATES, label='State or
> Province')
>         country = forms.ChoiceField(choices=COUNTRIES)
>         zip_code = forms.CharField(max_length=5)
>         birth_date = forms.DateField()
>         areas = forms.ModelMultipleChoiceField(queryset=Area.objects.all(),
> label='Preferred Areas', help_text='Select the areas that you\'d like
> to serve.')
>
> # areas.model
>
> from django.db import models
>
> 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()

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