Django告诉您找不到带有给定子弹的对象(即404代码)。虽然返回的queryset不为空
class PollDetailView(RetrieveAPIView):
serializer_class = PollSerializer
def get_queryset(self):
slug = self.kwargs['pk']
print(Poll.objects.filter(slug=slug)) # Prints '<QuerySet [<Poll: ddd>]>' reaching '/api/poll/ddd/' url
return Poll.objects.filter(slug=slug) # 404 Not Found
答案 0 :(得分:0)
在您的PollDetailView
look_up = 'slug'
并在您的urls.py
将PollDetailView
的网址更改为
url(r'^api/polls/(?P<slug>[\w-]+)/$', views.PollDetail.as_view(),name='poll-detail'),