Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate ChoiceField from database

Tags:

I'd like to have several fields in my form being rendered as ChoiceFields which get their content from the database.

I was thinking something like:

class SeriesForm(ModelForm):
  series = forms.ChoiceField(choices=Series.objects.all())

  class Meta:
    model = Series
    exclude = ('model', 'date_added',)

But the field series is now not appearing at all in my form. What am I missing?

After trying the solution (using the ModelChoiceField), I'm still seeing the same issue. Here is my code:

series = forms.ModelChoiceField(queryset=Series.objects.values('series'), 
  empty_label="     ")
like image 731
Stephen Avatar asked Feb 02 '10 12:02

Stephen


1 Answers

Use a ModelChoiceField instead.

like image 98
Ignacio Vazquez-Abrams Avatar answered Nov 12 '22 17:11

Ignacio Vazquez-Abrams