The repository or configuration that created this release differ from a previous release. View details.
dmr-jsonapi
JSON:API support for django-modern-rest.
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 thatdjango-modern-restsupports - Every error, including the ones that
django-modern-restraises by itself, becomes a JSON:API error document withstatus,code,detail, andsource - Follows the media type rules of the specification:
415for unsupportedContent-Typeparameters,406for unsupportedAcceptparameters - 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
JsonApiParserandJsonApiRendererby default. Both usemsgspecwhen it is installed, same asdjango-modern-rest. - Responds with
415if a request body hasContent-Typeparameters other thanextandprofile. Passallowed_params=toJsonApiParserif you need to relax it, for example to acceptcharset. - Responds with
406if every JSON:API media type inAccepthas parameters other thanextandprofile. - Uses
ErrorDocumentas 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, andfilterquery parameters, you can describe them with regularQuerycomponents - Endpoints for relationships (
/users/1/relationships/posts/) - Negotiation of
extandprofilemedia type parameters, they are accepted, but ignored - JSON:API errors for
404and500handlers of the whole project,build_404_handlerandbuild_500_handlerreturn the default format
Development
We use uv and just, see CONTRIBUTING.md.
License
Metadata
Release files for dmr-jsonapi 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Change of provenance
The repository or configuration that created these release files differ from that of a previous release.
We recommend inspecting the repository and configuration before proceeding. View details.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dmr_jsonapi-0.1.1.tar.gz | 17.3 kB | With provenance | Details for dmr_jsonapi-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dmr_jsonapi-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for dmr_jsonapi-0.1.1-py3-none-any.whl |
Total release size: 35.3 kB
Release files / dmr_jsonapi-0.1.1.tar.gz
Change of provenance
This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.
We recommend inspecting the repository and configuration before proceeding. View details.
| Download URL | dmr_jsonapi-0.1.1.tar.gz |
|---|---|
| Size | 17.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ed704224833adfe7cf5b88c447b2107dda235414564bcb4db85ec3be1e4fdcf
|
|
BLAKE2b-256 checksum How to use checksums |
c02bc144c40eec66f81da13b6a076834a576f780f90cda312f79be15bdf4a90b
|
| 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 logRelease files / dmr_jsonapi-0.1.1-py3-none-any.whl
Change of provenance
This file belongs to a release with changed provenance: the repository or configuration that created the release files differ from that of a previous release.
We recommend inspecting the repository and configuration before proceeding. View details.
| Download URL | dmr_jsonapi-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6adf6c244084dc812a44464aff12d3595d9291246935f6c56452999ab5ac0a6d
|
|
BLAKE2b-256 checksum How to use checksums |
5a80824e288085561f1c2badaaabd9142bb420fdf9c4f57265da995536f8e60b
|
| 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 logSecurity for dmr-jsonapi 0.1.1
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.
Change of provenance
The configuration that created this release differs from a previous release.
While this could reflect a routine change by the project maintainers, it may also indicate that the release files have been compromised.
Attestation for dmr_jsonapi-0.1.1.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 logA 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.