Skip to main content

JSON:API toolkit for Django Ninja

Project description

django-ninja-jsonapi

JSON:API toolkit for Django Ninja.

CI Package

This project ports the core ideas of fastapi-jsonapi to a Django Ninja + Django ORM stack, following the JSON:API specification.

Full documentation is available in docs/index.md.

Status

  • Working baseline for resource registration and route generation (GET, GET LIST, POST, PATCH, DELETE).
  • Strict query parsing for JSON:API-style filter, sort, include, fields, and page parameters.
  • JSON:API exception payload handling.
  • Atomic operations endpoint wiring (/operations).
  • Django ORM data-layer baseline for CRUD + basic relationship handling.
  • Top-level/resource/relationship links in responses.
  • Django ORM include optimization (select_related/prefetch_related split) with optional include mapping overrides.
  • Logical filter groups (and/or/not) and cursor pagination (page[cursor]).

Requirements

  • Python 3.10+
  • Django 4.2+
  • Django Ninja 1.0+

Install

uv add django-ninja-jsonapi

or

  • pip install django-ninja-jsonapi
  • poetry add django-ninja-jsonapi
  • pdm add django-ninja-jsonapi

Quick start

1) Define a Django model and a schema

from django.db import models
from pydantic import BaseModel


class Customer(models.Model):
    name = models.CharField(max_length=128)


class CustomerSchema(BaseModel):
    name: str

2) Create a JSON:API view class

from django_ninja_jsonapi import ViewBaseGeneric


class CustomerView(ViewBaseGeneric):
    pass

3) Register resources with ApplicationBuilder

from ninja import NinjaAPI

from django_ninja_jsonapi import ApplicationBuilder

api = NinjaAPI()
builder = ApplicationBuilder(api)

builder.add_resource(
    path="/customers",
    tags=["customers"],
    resource_type="customer",
    view=CustomerView,
    model=Customer,
    schema=CustomerSchema,
)

builder.initialize()

4) Mount API in Django URLs

from django.urls import path
from .api import api

urlpatterns = [
    path("api/", api.urls),
]

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",
}

Additional view/schema options:

  • django_filterset_class on a ViewBaseGeneric subclass to enable optional django-filter integration.
  • JSONAPIMeta.meta_fields (or Meta.meta_fields) on schema classes to expose selected fields in resource meta.

Exported public API

from django_ninja_jsonapi import ApplicationBuilder, QueryStringManager, HTTPException, BadRequest, ViewBaseGeneric

Development

uv run ruff format src tests
uv run ruff check src tests
uv run pytest --cov=src/django_ninja_jsonapi --cov-report=term-missing

See CONTRIBUTING.md for the full contribution workflow.

Release process

Releases are automated with GitHub Actions:

  1. Merge conventional-commit PRs into main.
  2. Release Please opens or updates a release PR with version bump + changelog updates.
  3. Merge the release PR to create a GitHub Release.
  4. Publish to PyPI runs on release: published and uploads the built package to PyPI.

Workflows:

  • .github/workflows/release-please.yml
  • .github/workflows/publish.yml

Required repository secrets:

  • REPO_ADMIN_TOKEN (used by Release Please)
  • PYPI_API_TOKEN (used for PyPI publishing)

Test coverage

Current tests cover:

  • Application builder initialization and route registration behavior
  • Query-string parsing behavior
  • Django ORM query-building mapping (filter/sort translation)
  • Exception handler response shape

Notes

  • This project is Django Ninja + Django ORM focused.
  • SQLAlchemy-specific modules have been removed to keep the codebase simpler and consistent.
  • CI runs on pull requests and pushes to main.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_ninja_jsonapi-0.3.0.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ninja_jsonapi-0.3.0-py3-none-any.whl (56.1 kB view details)

Uploaded Python 3

File details

Details for the file django_ninja_jsonapi-0.3.0.tar.gz.

File metadata

  • Download URL: django_ninja_jsonapi-0.3.0.tar.gz
  • Upload date:
  • Size: 36.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_ninja_jsonapi-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2d1ab97209bd52daad78deb5fd65f94f51e78dad394882c3741e2d179a52ae7b
MD5 2403b17c50843b2f2cc4987935288a73
BLAKE2b-256 9419fe7e3b4fb38afcde922ec323b4672a787eaf198e8708961025dd13850aa5

See more details on using hashes here.

File details

Details for the file django_ninja_jsonapi-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ninja_jsonapi-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67c978f4ae38381f55c11ee1a5149af92664156126240a6d20d7281b2404e228
MD5 4a14696e01ad16e77aa69f16434bdc83
BLAKE2b-256 59a6f173f062abb49495a3d7d7efddb3ae639925c557608983c2b0dcc83e56ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page