rom
rom

Reputation: 3672

django ModelChoiceField: display field names

I would like to display all the field names of a table in a drop down list. Here is my form:

sortFields=forms.ModelChoiceField(queryset=ActsInformationModel._meta.get_all_field_names(), empty_label="Select the sort field")

I get the error 'list' object has no attribute 'all'.

What's wrong?

Thanks in advance,

Romain

Upvotes: 0

Views: 1553

Answers (1)

Aamir Rind
Aamir Rind

Reputation: 39659

You need a ChoiceField here:

choices = [(i, i) for i in ActsInformationModel._meta.get_all_field_names()]

sortFields = forms.ChoiceField(label='Sort',
                               choices=choices)

Upvotes: 1

Related Questions