Complete worked example
python
from drf_lab import setup
setup()
from rest_framework import generics, serializers
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.test import APIRequestFactory
from drf_lab import reset_fixture
from drf_lab.models import Role
class PublicRole(serializers.ModelSerializer):
class Meta:
model = Role
fields = ["id", "title", "country"]
class SmallPage(LimitOffsetPagination):
default_limit = 1
max_limit = 2
class RoleList(generics.ListAPIView):
serializer_class = PublicRole
pagination_class = SmallPage
def get_queryset(self):
return Role.objects.filter(owner__username="alice", country=self.request.query_params.get("country", "UA")).order_by("id")
def inspect_example(query):
reset_fixture()
response = RoleList.as_view()(APIRequestFactory().get("/roles/", query))
return {"count": response.data["count"], "ids": [row["id"] for row in response.data["results"]],
"next": response.data["next"] is not None, "previous": response.data["previous"] is not None}- Reasoning
- The generic list view applies pagination to the queryset and uses the public serializer for the page. DRF returns count, next, previous and results. The helper reports row IDs and whether navigation links exist, keeping page membership and collection size separately observable. A request for limit=99 is bounded by max_limit rather than granting unlimited rows. This backend clamps some values to its configured limit; do not teach it as a universal validation-error policy. Request normalization behavior belongs in the documented API contract.