Afzal S.H.
Afzal S.H.

Reputation: 1084

Django AttributeError: 'str' object has no attribute 'model'

I have the below form;

class RemoveMemberForm(Form):
    member = forms.ModelChoiceField(queryset="",
                                  empty_label='Choose a Member',
    )

And the below views;

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()
        form2 = RemoveMemberForm()
        form2.fields['member'].queryset = station.members.all()
        return render(request, 
                      "home_station.html",
                      {"station":station,
                       "form1":form1,
                       "form2":form2,
                       "channels":channels,
                       "members":members,
                   },
                  )

class MemberRemove(View):
    def post(self, request, pk):
        form = RemoveMemberForm(request.POST)
        if form.is_valid():
            Station.objects.get(pk=pk).members.remove(
                form.cleaned_data['member']
            )
            return HttpResponseRedirect(reverse("home_station",
                                        kwargs={'pk':pk},
                                    )
                            )

What I am trying to do is have the second view delete the selected member and redirect to the first view. But instead I'm stuck at AttributeError at /station/2/removemember, the URL corresponding to the second view, 'str' object has no attribute 'model'

Upvotes: 2

Views: 6832

Answers (2)

Naman
Naman

Reputation: 183

You cannot have an empty queryset, change that.

Upvotes: 4

Luis Masuelli
Luis Masuelli

Reputation: 12323

This is because you specified:

queryset=""

In your form. Use a queryset instead (e.g. queryset=Member.objects.all()).

Upvotes: 8

Related Questions