The DRF View docs have this example:
from django.contrib.auth.models import User
from myapp.serializers import UserSerializer
from rest_framework import generics
from rest_framework.permissions import IsAdminUser
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
permission_classes = [IsAdminUser]
From reading the Django Docs, whenever .all() is encountered, it hits the database.
This means I suspect the DB is being hit when the view id being created, which seems not ideal to me.
The source code at least shows that it revaluates the queryset, so its not just using the cached versoin when instantiated:
assert self.queryset is not None, (
"'%s' should either include a `queryset` attribute, "
"or override the `get_queryset()` method."
% self.__class__.__name__
)
queryset = self.queryset
if isinstance(queryset, QuerySet):
# Ensure queryset is re-evaluated on each request.
queryset = queryset.all()
return queryset
My question is, wouldn't it be better to set the queryset in their example as follows:
class UserList(generics.ListCreateAPIView):
queryset = User.objects # <--- i.e. drop the .all()
...
This way it doesnt hit the DB at start up.