API pagination

This commit is contained in:
Klaas van Schelven
2025-09-11 15:09:42 +02:00
parent 30ae7881aa
commit 07b792775a
7 changed files with 266 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
from django.shortcuts import get_object_or_404
from rest_framework import viewsets
from bugsink.api_pagination import AscDescCursorPagination
from .models import Project
from .serializers import (
@@ -9,6 +10,14 @@ from .serializers import (
)
class ProjectPagination(AscDescCursorPagination):
# Cursor pagination requires an indexed, mostly-stable ordering field. We use `name`, which is indexed; for Teams,
# updates are rare and the table is small, so "requirement met in practice though not in theory".
base_ordering = ("name",)
page_size = 250
default_direction = "asc"
class ProjectViewSet(viewsets.ModelViewSet):
"""
/api/canonical/0/projects/
@@ -20,6 +29,7 @@ class ProjectViewSet(viewsets.ModelViewSet):
"""
queryset = Project.objects.all()
http_method_names = ["get", "post", "patch", "head", "options"]
pagination_class = ProjectPagination
def filter_queryset(self, queryset):
if self.action != "list":
@@ -34,8 +44,7 @@ class ProjectViewSet(viewsets.ModelViewSet):
if team_id:
qs = qs.filter(team=team_id)
# Explicit ordering aligned with UI
return qs.order_by("name")
return qs
def get_object(self):
# Pure PK lookup (bypass filter_queryset)