Coverage for core\pagination.py: 0%
29 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-10-17 16:50 +0800
« prev ^ index » next coverage.py v7.16.0, created at 2026-10-17 16:50 +0800
1from typing import Generic, List, TypeVar, Optional
2from pydantic import BaseModel
3from ninja.pagination import PaginationBase
4from django.db.models import QuerySet
6T = TypeVar('T')
8class PageResponse(BaseModel, Generic[T]):
9 items: List[T]
10 total: int
11 page: int
12 size: int
13 pages: int
15class StandardPagination(PaginationBase):
16 class Input(BaseModel):
17 page: int = 1
18 size: int = 20
20 class Output(BaseModel, Generic[T]):
21 items: List[T]
22 total: int
23 page: int
24 size: int
25 pages: int
27 def paginate_queryset(self, queryset: QuerySet, pagination: Input, **params):
28 page = max(1, pagination.page)
29 size = min(max(1, pagination.size), 100)
30 total = queryset.count()
31 pages = (total + size - 1) // size if total > 0 else 1
33 offset = (page - 1) * size
34 items = list(queryset[offset:offset + size])
36 return {
37 'items': items,
38 'total': total,
39 'page': page,
40 'size': size,
41 'pages': pages,
42 }