Skip to content Skip to sidebar Skip to footer

Getting Object Not Iterable Error In Django Form

IN my form i have this mychoices = User.objects.filter(category__name='city') I get this error User Object is not iterable. I am new to django This is the next line relevance = f

Solution 1:

You need to specify only widget class, not calling constructor:

relevance = forms.MultipleChoiceField(choices=mychoices, widget=forms.CheckboxSelectMultiple)

UPDATE Choices must be iterable of 2-tuples. First will be value that will be returned in POST request parameters, second - string representation displayed on UI. May be, it makes sense to do something like this:

choices = User.objects.filter(category__name='city').values_list('id', 'first_name')

You will get:

(1, 'Mark')
(2, 'Jack')
...

When user selects option and post form, you will receive user ID in parameters, so you will be able to retrieve user object by it.

Post a Comment for "Getting Object Not Iterable Error In Django Form"