djangochoicefield

Django ChoiceField populated from database values


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?


Solution

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