How to use restriction in django rest framework generics.RetrieveAPIView

As generics.RetrieveAPIView in django rest framework should return only one record, I would like to use the restriction in the receive request method as below

class PortUserView(generics.RetrieveAPIView): lookup_field = 'user' def get_queryset(self): return PortUser.objects.all()[:1] 

get an error like this " Unable to filter the request after the slice has been done .

What is wrong with my code?

+4
source share
4 answers

You do not need to worry about returning a single object from a set of queries during retrieve . DRF will handle this automatically using the .get_object() defined in GenericAPIView .

You can simply use the code below 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 instance of the object that should be used for detailed representations. By default, the lookup_field parameter is used to filter the QuerySet database.

Source code for the 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 see that DRF uses the .get_object() function to get an object from a set of queries. To perform filtering, use 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 
+6
source

You do not need to do this. Instead, you should let the framework filter your request:

 class PortUserView(generics.RetrieveAPIView): queryset = PortUser.objects.all() 
+1
source
 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 
+1
source

RetrieveAPIView by default returns a single object. If multiple objects are returned, an exception will be thrown. You just need to define the query in your view.

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

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

0
source

All Articles