5

I am writing an app in django rest-framework: My views.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)

I want to return error status code if query returns None. But the problem if i try to set Response it throws error:

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

Else if query result is Not None it is working. How can i set status code on Django rest-framework.

2 Answers2

5

The method is expected to return a QuerySet, not a Response object, my bet is that you should throw an Exception, either an APIException or an Http404.

Anyway your handling seems odd, I think you should just return the QuerySet and the framework will handle if the result is empty or not. The method should look like this:

def get_queryset(self):
    return tags.objects.filter(tag='burger')
Claude Vedovini
  • 2,421
  • 21
  • 19
  • I am actually verifying the APIkey and if it valid the i will return the result otherwise what should i do.....Can you tell... –  Apr 16 '13 at 18:06
  • `class couponsList(generics.ListCreateAPIView,APIView): model = coupons def get_object(self,key): try: return app.objects.filter(appKey=key).exists() except app.DoesNotExist: raise Http404 def get(self,request,format=None): key = self.request.QUERY_PARAMS.get('AppKey', None) self.get_object(key) da = coupons.objects.all() serializer_class = getAllCouponsDetailSerializer(da) return Response(serializer_class.data)`try & except not working.Please Help.I want to check Apikey is valid then only proceed –  Apr 17 '13 at 06:13
  • 1
    Don't know why this was down-voted, as the answer is correct. Additionally, if you want to deal with 404 cases, raise an `Http404` exception. – Tom Christie Apr 17 '13 at 09:35
  • if you have some APIKey that you should be validating then you must send an Exception. – Claude Vedovini Apr 21 '13 at 09:35
  • about the 404 I am not sure, you are returning a list of objects then just return an empty list, not a 404, I would return a 404 if the API was expected to return a single object. But then again, it should be the job of the framework to deal with what Http response must be sent back, you deal at the model level, you should only send objects or business exceptions – Claude Vedovini Apr 21 '13 at 09:38
2

Can you try this

model = tags # Model name
serializer_class = getAllTagsDetailSerializer # Call serializer

def get_queryset(self):
    key = self.request.QUERY_PARAMS.get('appKey', None)
    getTagName = self.request.QUERY_PARAMS.get('tagName')
    keyData = app.objects.filter(appKey=key).exists()    
    try:
        if keyData == True:
            return tags.objects.filter(tag=getTagName)
        else:
            raise exceptions.PermissionDenied
    except app.DoesNotExist:
        pass

I think it will work....

Vaibhav Jain
  • 5,287
  • 10
  • 54
  • 114