开发者

Why do I get an object is not iterable error?

Why do I get the following error in my app

Caught TypeError while rendering: 'ModelNameHere' object is not iterable

but I don't get it when I execute it from the s开发者_如何学JAVAhell?

I just have a custom field in my form which inherits from forms.ModelForm

custom_serving_size = forms.ChoiceField(
    ServingSize.objects.all(),
    widget=forms.Select(attrs={'class':'ddl'})
)

EDIT

This is my form class

class RecipeIngredientForm(forms.ModelForm):
    serving_size = forms.ChoiceField(choices=ServingSize.objects.all())

The error happens on ServingSize.objects.all()


custom_serving_size = forms.ChoiceField(
    ServingSize.objects.all(),
    widget=forms.Select(attrs={'class':'ddl'})
)

this has to be

custom_serving_size = forms.ModelChoiceField(
    queryset=ServingSize.objects.all(),
    widget=forms.Select(attrs={'class':'ddl'})
)

or

custom_serving_size = forms.ChoiceField(
    choices=[(obj.id, `text user sees`) for obj in ServingSize.objects.all()],
    widget=forms.Select(attrs={'class':'ddl'})
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜