简体   繁体   中英

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'

This is because you specified:

queryset=""

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

您不能有一个空的查询集,更改它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM