return response in django rest-framework

10,063

Solution 1

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....

Solution 2

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')
Share:
10,063
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • Admin
    Admin about 11 years
    I am actually verifying the APIkey and if it valid the i will return the result otherwise what should i do.....Can you tell...
  • Admin
    Admin about 11 years
    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
  • Tom Christie
    Tom Christie about 11 years
    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.
  • Claude Vedovini
    Claude Vedovini about 11 years
    if you have some APIKey that you should be validating then you must send an Exception.
  • Claude Vedovini
    Claude Vedovini about 11 years
    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