我正在django rest框架中编写一个应用程序: 我的观点.py:
class tagList(generics.ListCreateAPIView,APIView):
model = tags
serializer_class = getAllTagsDetailSerializer
def get_queryset(self):
print "q1"
print self.request.QUERY_PARAMS.get('tag', None)
print self.request.user
print "q1"
if tags.objects.filter(tag='burger')!= None:
return tags.objects.filter(tag='burger')
else:
content = {'please move along': 'nothing to see here'}
return Response(content, status=status.HTTP_404_NOT_FOUND)
如果查询未返回任何值,则返回错误状态代码。 但问题是,如果我试图设置响应,它会抛出错误:
Exception Type: TypeError
Exception Value:
object of type 'Response' has no len()
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/paginator.py in _get_count, line 53
否则,如果查询结果不是None,则它将正常工作。 如何在Django rest框架上设置状态代码。
你能试试这个吗
我想它会起作用的。。。。
方法应该返回一个QuerySet,而不是一个响应对象,我敢打赌您应该抛出一个
Exception
,或者是APIException
,或者是Http404
。不管怎样,您的处理看起来很奇怪,我认为您应该只返回QuerySet,如果结果是空的或者不是空的,框架将处理。方法应该如下所示:
相关问题 更多 >
编程相关推荐