JSON:API toolkit for Django Ninja
Project description
django-ninja-jsonapi
JSON:API toolkit for Django Ninja.
This project brings JSON:API specification support to Django Ninja.
Full documentation is available at ignacemaes.com/django-ninja-jsonapi.
Status
- Transparent JSON:API wrapping — write plain Django Ninja views, get JSON:API documents automatically.
- Auto-detected relationships from Pydantic schema type hints.
- Strict query parsing for JSON:API-style
filter,sort,include,fields, andpageparameters. - JSON:API exception payload handling.
- Content-type negotiation (415/406) per the JSON:API spec.
- Attribute key inflection (
dasherizeorcamelize).
Requirements
- Python 3.10+
- Django 4.2+
- Django Ninja 1.0+
Install
uv add django-ninja-jsonapi
or
pip install django-ninja-jsonapipoetry add django-ninja-jsonapipdm add django-ninja-jsonapi
Quick start
1) Define a Django model and schemas
from typing import ClassVar
from django.db import models
from pydantic import BaseModel
from django_ninja_jsonapi import JsonApiMeta
class Customer(models.Model):
name = models.CharField(max_length=128)
class CustomerSchema(BaseModel):
id: int
name: str
jsonapi_meta: ClassVar[JsonApiMeta] = JsonApiMeta(resource_type="customers")
class CustomerCreateSchema(BaseModel):
name: str
2) Create your API with NinjaJsonAPI
from django_ninja_jsonapi import NinjaJsonAPI
api = NinjaJsonAPI()
@api.get("/customers", response=list[CustomerSchema])
def list_customers(request):
return Customer.objects.all()
@api.get("/customers/{customer_id}", response=CustomerSchema)
def get_customer(request, customer_id: int):
return Customer.objects.get(pk=customer_id)
@api.post("/customers", response={201: CustomerSchema})
def create_customer(request, body: CustomerCreateSchema):
customer = Customer.objects.create(**body.model_dump())
return 201, customer
3) Mount API in Django URLs
from django.urls import path
from .api import api
urlpatterns = [
path("api/", api.urls),
]
Example response
GET /api/customers/1
{
"data": {
"type": "customers",
"id": "1",
"attributes": {
"name": "Alice"
}
}
}
GET /api/customers
{
"data": [
{
"type": "customers",
"id": "1",
"attributes": { "name": "Alice" }
},
{
"type": "customers",
"id": "2",
"attributes": { "name": "Bob" }
}
]
}
Resources have a type and id at the top level while model fields are nested under attributes.
Relationships, includes, sparse fieldsets, filtering, sorting and pagination all follow the JSON:API specification.
Architecture
NinjaJsonAPI is a NinjaAPI subclass that transparently wraps responses in JSON:API documents and unwraps JSON:API request bodies into plain Pydantic schemas.
flowchart LR
Client -->|HTTP request| NinjaJsonAPI
NinjaJsonAPI --> ContentNegotiation["Content Negotiation\n(415 / 406)"]
ContentNegotiation --> Unwrap["Unwrap JSON:API body\n→ plain Pydantic schema"]
Unwrap --> Endpoint["Your endpoint\n(plain Django Ninja view)"]
Endpoint -->|"dict / Pydantic / QuerySet"| JSONAPIRenderer
JSONAPIRenderer -->|"application/vnd.api+json"| Client
Configuration
Set JSON:API options in Django settings:
NINJA_JSONAPI = {
"MAX_INCLUDE_DEPTH": 3,
"MAX_PAGE_SIZE": 20,
"ALLOW_DISABLE_PAGINATION": True,
"INCLUDE_JSONAPI_OBJECT": False,
"JSONAPI_VERSION": "1.0",
"INFLECTION": "dasherize", # or "camelize", or None (default)
}
Exported public API
from django_ninja_jsonapi import (
NinjaJsonAPI,
JsonApiMeta,
ModelSchema,
JSONAPIRenderer,
QueryStringManager,
HTTPException,
BadRequest,
NotFound,
apply_attributes,
get_rel_id,
get_rel_ids,
jsonapi_paginate,
jsonapi_pagination,
jsonapi_cursor_pagination,
jsonapi_include,
jsonapi_meta,
jsonapi_links,
jsonapi_sort,
jsonapi_filter,
parse_include,
)
Contributing
See CONTRIBUTING.md for setup, local checks, contribution workflow, and maintainer release notes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_ninja_jsonapi-0.9.1.tar.gz.
File metadata
- Download URL: django_ninja_jsonapi-0.9.1.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
639710c5a57569dd5e53d6ccb8d4c9db570b8fa99eff3127e1bb32e9cef9a52d
|
|
| MD5 |
c39646aa842fcd74afcc856f08c42c08
|
|
| BLAKE2b-256 |
4131f6bda287bde48c779a5cc9fcf9394d629e724b994826d8d1417ca1196ec1
|
File details
Details for the file django_ninja_jsonapi-0.9.1-py3-none-any.whl.
File metadata
- Download URL: django_ninja_jsonapi-0.9.1-py3-none-any.whl
- Upload date:
- Size: 41.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca097b934e2e8615ead791f6df16913e02eb0b52ce574da4c7b2b620afb3a4d6
|
|
| MD5 |
d42f2019cf5e42e53a5d4a415edbcb6d
|
|
| BLAKE2b-256 |
2d5bd9cf8f62717660248db08a1b0b951d72b46fdbcb359ff3e1b6e16b200b7f
|