Skip to main content

dmr-jsonapi

JSON:API support for django-modern-rest.

Tests Result PyPI Supported Python Version Code Style

Features

  • Parses and renders application/vnd.api+json
  • Typed models for resources, relationships, links, documents, and errors, they work with pydantic, msgspec, and any other serializer that django-modern-rest supports
  • Every error, including the ones that django-modern-rest raises by itself, becomes a JSON:API error document with status, code, detail, and source
  • Follows the media type rules of the specification: 415 for unsupported Content-Type parameters, 406 for unsupported Accept parameters
  • Helpers to build documents and pagination links
  • OpenAPI schema with the right media types and error documents
  • Fully typed, 100% test coverage, same tools and style as django-modern-rest

Installation

pip install dmr-jsonapi

Works with django-modern-rest from 0.15 to 0.17 and Python 3.11 and newer.

Quick start

Describe your resource, your document, and use JsonApiController instead of a regular Controller:

>>> from typing_extensions import TypedDict

>>> from dmr import Path
>>> from dmr.plugins.pydantic import PydanticSerializer
>>> from dmr_jsonapi import JsonApiController
>>> from dmr_jsonapi.builders import document, resource_object
>>> from dmr_jsonapi.objects import DocumentEnvelope, ResourceObject

>>> class UserAttributes(TypedDict):
...     name: str

>>> class UserPath(TypedDict):
...     user_id: int

>>> class UserDocument(DocumentEnvelope):
...     data: ResourceObject[UserAttributes]

>>> class UserController(JsonApiController[PydanticSerializer]):
...     def get(self, parsed_path: Path[UserPath]) -> UserDocument:
...         attributes: UserAttributes = {'name': 'Zamenhof'}
...         return document(
...             resource_object('users', parsed_path['user_id'], attributes),
...         )

Call it like any other controller. The response has the JSON:API media type:

>>> from dmr.test import DMRRequestFactory

>>> request = DMRRequestFactory().get('/users/1/')
>>> response = UserController.as_view()(request, user_id=1)
>>> response.status_code
200
>>> response['Content-Type']
'application/vnd.api+json'
>>> response.content
b'{"data":{"type":"users","id":"1","attributes":{"name":"Zamenhof"}}}'

Name your documents

Define documents by subclassing DocumentEnvelope and adding data, like UserDocument above. Do not use Document[ResourceObject[...]] as a top level request or response type. Schema generators name every specialization of a generic TypedDict the same, so two of them clash in your OpenAPI schema. Python also can't substitute type arguments when you subclass a specialized generic TypedDict, so validation would silently go away.

Document[...] is still handy to annotate your own helpers, the documents that you define this way are compatible with it.

Errors

All errors that django-modern-rest produces (validation, 404, 406, 405, throttling, authentication, and so on) are rendered as JSON:API errors:

>>> import json

>>> class ArticleController(JsonApiController[PydanticSerializer]):
...     def get(self) -> UserDocument:
...         raise NotImplementedError

>>> request = DMRRequestFactory().delete('/articles/')
>>> response = ArticleController.as_view()(request)
>>> response.status_code
405
>>> json.loads(response.content)['errors'][0]['status']
'405'

Validation errors point to the broken place in the request:

{
  "errors": [
    {
      "status": "400",
      "code": "value_error",
      "detail": "Input should be a valid string",
      "source": {"pointer": "/data/attributes/name"}
    }
  ]
}

Locations of the body become JSON pointers, locations of query parameters and headers become source.parameter and source.header.

To return your own errors, raise JsonApiError anywhere in your API:

>>> from http import HTTPStatus
>>> from dmr import ResponseSpec
>>> from dmr_jsonapi import JsonApiError
>>> from dmr_jsonapi.errors import error_object
>>> from dmr_jsonapi.objects import ErrorDocument

>>> class MissingUserController(JsonApiController[PydanticSerializer]):
...     responses = (
...         ResponseSpec(ErrorDocument, status_code=HTTPStatus.NOT_FOUND),
...     )
...
...     def get(self) -> UserDocument:
...         raise JsonApiError(
...             error_object(
...                 'User does not exist',
...                 code='user_not_found',
...                 source={'parameter': 'user_id'},
...             ),
...             status_code=HTTPStatus.NOT_FOUND,
...         )

>>> response = MissingUserController.as_view()(DMRRequestFactory().get('/'))
>>> response.status_code
404
>>> json.loads(response.content)
{'errors': [{'status': '404', 'code': 'user_not_found', 'detail': 'User does not exist', 'source': {'parameter': 'user_id'}}]}

status is always filled in for you and always matches the HTTP status code. Like with any django-modern-rest response, declare the status codes that you return, unless you turn off response validation. Several error objects can be passed to one JsonApiError.

Documents and pagination

dmr_jsonapi.builders has small functions for the repetitive parts: resource_identifier, resource_object, relationship_to_one, relationship_to_many, document, and pagination_links. They only include members that you pass, ids are converted to strings.

>>> from django.test import RequestFactory
>>> from dmr_jsonapi.builders import pagination_links

>>> request = RequestFactory().get('/users/', {'page[number]': '2'})
>>> links = pagination_links(request, number=2, size=10, total=25)
>>> links['next']
'http://testserver/users/?page%5Bnumber%5D=3&page%5Bsize%5D=10'
>>> links['last']
'http://testserver/users/?page%5Bnumber%5D=3&page%5Bsize%5D=10'

What JsonApiController does

  • Uses JsonApiParser and JsonApiRenderer by default. Both use msgspec when it is installed, same as django-modern-rest.
  • Responds with 415 if a request body has Content-Type parameters other than ext and profile. Pass allowed_params= to JsonApiParser if you need to relax it, for example to accept charset.
  • Responds with 406 if every JSON:API media type in Accept has parameters other than ext and profile.
  • Uses ErrorDocument as the error model in the OpenAPI schema.

You can use the parser and the renderer without the controller too, add them to parsers and renderers of any django-modern-rest controller or to the DMR_SETTINGS.

Not supported yet

  • Parsing of include, fields[...], sort, and filter query parameters, you can describe them with regular Query components
  • Endpoints for relationships (/users/1/relationships/posts/)
  • Negotiation of ext and profile media type parameters, they are accepted, but ignored
  • JSON:API errors for 404 and 500 handlers of the whole project, build_404_handler and build_500_handler return the default format

Development

We use uv and just, see CONTRIBUTING.md.

License

MIT

Metadata

Release files for dmr-jsonapi 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dmr-jsonapi 0.1.0
File Size Uploaded Provenance Actions
dmr_jsonapi-0.1.0.tar.gz 17.3 kB With provenance Details for dmr_jsonapi-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dmr-jsonapi 0.1.0
File Interpreter ABI Platform Actions
dmr_jsonapi-0.1.0-py3-none-any.whl Python 3 none any Details for dmr_jsonapi-0.1.0-py3-none-any.whl

Total release size: 35.3 kB

Release files / dmr_jsonapi-0.1.0.tar.gz

Download URL dmr_jsonapi-0.1.0.tar.gz
Size 17.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d190393a1e4e70702c2cd29c601219124f5a04a68b490d8fa0356c54e71160ae
BLAKE2b-256 checksum
How to use checksums
0bf66d313fe83325649afe312224ae82a5a50b29b40b21f9b6b8d60f4ac3dd1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / dmr_jsonapi-0.1.0-py3-none-any.whl

Download URL dmr_jsonapi-0.1.0-py3-none-any.whl
Size 18.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
57553c4cd9c06017af73d464bfc03e6728ec0bb3fb815928f2c7cbdec0179913
BLAKE2b-256 checksum
How to use checksums
d1b0452677c48f738f93857b00d229d5b39fcc6afc7ef3ad1a67033de86cac39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for dmr-jsonapi 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for dmr_jsonapi-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page