python - Django TypeError 'User' object is not iterable -


is not possible iterate on user objects using user.objects.all()?? trying same no avail

i have form;

class addmemberform(form):     user = forms.choicefield(choices=user.objects.all(),                              initial='choose user',     )  

and trying render through template. corresponding part of views.py below;

class stationhome(view):     def get(self, request, pk):         station = station.objects.get(pk=pk)         channels = channel.objects.filter(station=station)         members = station.members.all()         form1 = addmemberform()         return render(request,                        "home_station.html",                       {"form1":form1,                        "station":station,                        "channels":channels,                        "members":members,                    },                   ) 

finally corresponding part of corresponding template,

<form method="post" action="{% url 'add_member' station.pk %}">     {% csrf_token %}     {{ form1 }} </form> 

but unable access url due form. typeerror at corresponding url 'user' object not iterable error.

someone please out.

use modelchoicefield instead of simple choicefield:

user = forms.modelchoicefield(queryset=user.objects.all(),                               empty_label="(choose user)") 

update: can change queryset in form's constructor. example if want exclude added members form:

class addmemberform(form):     ...     def __init__(self, *args, **kwargs):         station = kwargs.pop('station')         super(addmemberform, self).__init__(*args, **kwargs)         if station:             self.fields['user'].queryset = user.objects.exclude(                                              id__in=station.members.all()) 

and create form station argument:

form1 = addmemberform(station=station) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -