Django ChoiceField populated from database values

Oli picture Oli · Aug 13, 2012 · Viewed 23k times · Source

I am having problems using a ChoiceField to create a drop down list of values in the database. Here is the snippet of code

from django import forms
from testplatform.models import ServiceOffering

class ContactForm(forms.Form):

    subject = forms.ChoiceField(queryset=ServiceOffering.objects.all())
    #subject = forms.ModelMultipleChoiceField(queryset=ServiceOffering.objects.all())

The #subject.... line works, but when I use the line ChoiceField(queryset....) I get the following error.

__init__() got an unexpected keyword argument 'queryset'

Any ideas?

Answer

Chris Pratt picture Chris Pratt · Aug 13, 2012

ChoiceField doesn't have a queryset. You're looking for ModelChoiceField