This commit is contained in:
Ragheed Al-midani 2017-05-23 11:27:21 +00:00 committed by GitHub
commit 312207a12e

View File

@ -76,12 +76,19 @@ class GenericAPIView(views.APIView):
def get_object(self): def get_object(self):
""" """
Returns the object the view is displaying. Returns the object the view is displaying. The value is memoized across
the lifecycle of the view to reduce database hits when e.g. delegating
from custom actions in a ModelViewSet to built-in actions.
You may want to override this if you need to provide non-standard You may want to override this if you need to provide non-standard
queryset lookups. Eg if objects are referenced using multiple queryset lookups. Eg if objects are referenced using multiple
keyword arguments in the url conf. keyword arguments in the url conf.
""" """
# Check if obj has been looked up (and therefore memoized) before.
obj = getattr(self, 'obj', None)
if obj is not None:
return obj
queryset = self.filter_queryset(self.get_queryset()) queryset = self.filter_queryset(self.get_queryset())
# Perform the lookup filtering. # Perform the lookup filtering.
@ -100,6 +107,9 @@ class GenericAPIView(views.APIView):
# May raise a permission denied # May raise a permission denied
self.check_object_permissions(self.request, obj) self.check_object_permissions(self.request, obj)
# Memoize obj for potential lookups in the future.
self.obj = obj
return obj return obj
def get_serializer(self, *args, **kwargs): def get_serializer(self, *args, **kwargs):