When working with RetrieveAPIView queryset must be defined and usually it is defined as queryset = <Model>
.objects.all()
Why does retrieving a single instance require loading all the objects?
CodePudding user response:
You don't load all the objects. The queryset is not evaluated. The RetrieveAPIView
[classy-drf] will use the queryset as a base to make queries. Indeed, in the .retrieve(…)
method, it will obtain the object:
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) return Response(serializer.data)
and the .get_object(…)
method, it will fetch the single object:
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) # May raise a permission denied self.check_object_permissions(self.request, obj) return obj
It will thus for example boil down to get_object_or_404(MyModel.objects.all(), pk=42)
, and this will make a query to fetch the single record with primary key 42
. QuerySet
s are lazy: the MyModel.objects.all()
is not evaluated unless the queryset is consumed, but here it thus is used to construct a new queryset that will eventually fetch a single record.