2

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'

2 Answers 2

8

This is because you specified:

queryset=""

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

Sign up to request clarification or add additional context in comments.

Comments

4

You cannot have an empty queryset, change that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.