17.6. Ninja Pagination
Source: https://django-ninja.dev/guides/response/pagination/
Checkout: https://petersimpson.dev/blog/trying-out-django-ninja/
17.6.1. LimitOffsetPagination
/api/users?limit=10&offset=0
settings.NINJA_PAGINATION_PER_PAGE = 100
from ninja.pagination import paginate
@api.get('/users', response=List[UserSchema])
@paginate
def list_users(request):
return User.objects.all()
curl http://127.0.0.1:8000/api/users?limit=10&offset=0
17.6.2. PageNumberPagination
from ninja.pagination import paginate, PageNumberPagination
@api.get('/users', response=list[UserSchema])
@paginate(PageNumberPagination, page_size=10)
def list_users(request):
return User.objects.all()
curl http://127.0.0.1:8000/api/users?page=2
17.6.3. Use Case - 1
>>>
... from django.http import HttpRequest
... from ninja import Router
... from ninja.pagination import paginate, PageNumberPagination
... from shop.models import Product
... from shop.schemas import ProductSchema
...
...
... router = Router()
...
...
... @router.get('/products', response=list[ProductSchema])
... @paginate(PageNumberPagination, page_size=5)
... def products_list(request: HttpRequest):
... products = Product.objects.all().values('name', 'price', 'barcode')
... return list(products)
17.6.4. Assignments
# FIXME: Write tests
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Ninja Paginate
# - Difficulty: easy
# - Lines: 8
# - Minutes: 8
# %% English
# 0. Use `myproject.shop`
# 1. Create an endpoint `GET /api/v2/shop/products`
# 2. Endpoint should list all products in a database
# 3. Results should be paginated with limit and offset
# 4. Use package `ninja`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz endpoint `GET /api/v2/shop/products`
# 2. Endpoint ma wylistować wszystkie produkty w bazie danych
# 3. Wyniki mają być paginowane z limitem i offsetem
# 4. Użyj pakietu `ninja`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
...
# FIXME: Write tests
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: Django Ninja Paginate
# - Difficulty: easy
# - Lines: 8
# - Minutes: 8
# %% English
# 0. Use `myproject.shop`
# 1. Create an endpoint `GET /api/v2/shop/products`
# 2. Endpoint should list all products in a database
# 3. Results should be paginated by 10 products per page
# 4. Use package `ninja`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Stwórz endpoint `GET /api/v2/shop/products`
# 2. Endpoint ma wylistować wszystkie produkty w bazie danych
# 3. Wyniki mają być paginowane po 10 produktów na stronę
# 4. Użyj pakietu `ninja`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
...