dotty
dotty

Reputation: 41513

Django forms, getting a select box from a different model

Hay, say i have 2 models listing and category.

How would i use djangos form framework to automatically create a select drop down box for category?

class NewListingForm(forms.Form):
    name = forms.CharField(required=True)
    description = forms.CharField(widget=forms.Textarea, required=True)
    category  ... 

Upvotes: 0

Views: 2463

Answers (1)

JamesO
JamesO

Reputation: 25966

ModelChoiceField

category = forms.ModelChoiceField(queryset=Category.objects.all(), 
                                  empty_label="(Nothing)")

Upvotes: 6

Related Questions