为什么Django无法找到带有给定子弹的对象?

时间:2019-07-03 07:27:30

标签: python django django-rest-framework

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

1 个答案:

答案 0 :(得分:0)

在您的PollDetailView

中添加look_up字段
look_up = 'slug'

并在您的urls.py

PollDetailView的网址更改为

url(r'^api/polls/(?P<slug>[\w-]+)/$', views.PollDetail.as_view(),name='poll-detail'),