[Fixed]-How to use limit in django rest framework generics.RetrieveAPIView

22👍

There is no need for you to worry about returning a single object from the queryset at the time of retrieve. DRF will handle that automatically for you using its .get_object() defined in the GenericAPIView.

You can just use the below code and DRF will handle the retrieve action for you.

class PortUserView(generics.RetrieveAPIView):
    lookup_field = 'user'
    queryset = PortUser.objects.all()

get_object(self)

Returns an object instance that should be used for detail views.
Defaults to using the lookup_field parameter to filter the base
queryset.

Source code for retrieve action:

def retrieve(self, request, *args, **kwargs):
    instance = self.get_object() # here the object is retrieved
    serializer = self.get_serializer(instance)
    return Response(serializer.data)

We can see that DRF uses the .get_object() function to get the object from the queryset. To perform the filtering, it uses the lookup_field defined in the view.

Here is the actual get_object() code to make things more clear.

def get_object(self):
        """
        Returns the object the view is displaying.

        You may want to override this if you need to provide non-standard
        queryset lookups.  Eg if objects are referenced using multiple
        keyword arguments in the url conf.
        """
        queryset = self.filter_queryset(self.get_queryset())

        # Perform the lookup filtering.
        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field

        assert lookup_url_kwarg in self.kwargs, (
            'Expected view %s to be called with a URL keyword argument '
            'named "%s". Fix your URL conf, or set the `.lookup_field` '
            'attribute on the view correctly.' %
            (self.__class__.__name__, lookup_url_kwarg)
        )

        filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
        obj = get_object_or_404(queryset, **filter_kwargs) # <-- can see that filtering is performed on the base of 'lookup_field'

        # May raise a permission denied
        self.check_object_permissions(self.request, obj)

        return obj # will return the single retrieved object

2👍

You don’t need to do this. Instead you should let the framework filter your queryset:

class PortUserView(generics.RetrieveAPIView):
    queryset =  PortUser.objects.all()

1👍

def get_object(self):
    queryset = self.filter_queryset(self.get_queryset())
    lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field

    assert lookup_url_kwarg in self.kwargs, (
        'Expected view %s to be called with a URL keyword argument '
        'named "%s". Fix your URL conf, or set the `.lookup_field` '
        'attribute on the view correctly.' %
        (self.__class__.__name__, lookup_url_kwarg)
    )
    filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}

    # ##########################################################################################
    # obj = get_object_or_404(queryset, **filter_kwargs)

    queryset = _get_queryset(queryset)
    try:
        return queryset.get(**filter_kwargs)
    except AttributeError:
        klass__name = queryset.__name__ if isinstance(queryset, type) else queryset.__class__.__name__
        raise ValueError(
            "First argument to get_object_or_404() must be a Model, Manager, "
            "or QuerySet, not '%s'." % klass__name
        )
    except queryset.model.DoesNotExist:
        res = {"success": "false", "code": 404, "message": 'Product Not found'}
        raise CustomValidation(res, 404)
        # raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)

    # ##########################################################################################
    # May raise a permission denied

    self.check_object_permissions(self.request, obj)
    return obj
👤Rahil

0👍

RetrieveAPIView by default returns a single object. If multiple objects are returned, it would raise an exception. You just need to define queryset in you view.

class PortUserView(generics.RetrieveAPIView):
    lookup_field = 'user'
    queryset = PortUser.objects.all()
    url_kwarg = 'whatever_field_you_want_to_map'

Try to map the the lookup_field to a unique field in your model so that MultipleObjectsReturned exception is not raised inside the view.

Leave a comment