Overview

I had an opportunity to try Django REST framework JSON:API (DJA), so here are my notes.

https://django-rest-framework-json-api.readthedocs.io/en/stable/index.html

Installation

Launch the example app described on the following page.

https://django-rest-framework-json-api.readthedocs.io/en/stable/getting-started.html

git clone https://github.com/django-json-api/django-rest-framework-json-api.git
cd django-rest-framework-json-api
python3 -m venv env
source env/bin/activate
pip install -Ur requirements.txt
django-admin migrate --settings=example.settings
django-admin loaddata drf_example --settings=example.settings
django-admin runserver --settings=example.settings

As a result, the following screens were obtained.

Trying the Default Settings

List

Accessing the following URL displays the list of blogs.

http://localhost:8000/blogs

Sort

You can specify sorting from the “Filters” button on the screen. Accessing the following URL returns results sorted in ascending order by name.

http://localhost:8000/blogs?sort=name

Adding a minus sign returns results in descending order.

http://localhost:8000/blogs?sort=-name

Fields

You can specify which attributes to retrieve using fields. Accessing the following URL results in empty attributes.

http://localhost:8000/blogs?fields[blog]=aaa

Specifying the Number of Search Results

JsonApiPageNumberPagination

This is the default setting.

Accessing the following returns 1 blog out of 2 total blogs.

http://localhost:8000/blogs?page[size]=1

Adding number returns the next item.

http://localhost:8000/blogs?page[number]=2&page[size]=1

JsonApiLimitOffsetPagination

This method uses limit and offset for pagination. Change DEFAULT_PAGINATION_CLASS to rest_framework_json_api.pagination.JsonApiLimitOffsetPagination.

...
    "DEFAULT_PAGINATION_CLASS": "rest_framework_json_api.pagination.JsonApiLimitOffsetPagination", # "rest_framework_json_api.pagination.JsonApiPageNumberPagination",  # noqa: B950
...

The following URL retrieves the first item.

http://localhost:8000/blogs?page[limit]=1

The following URL retrieves the second item.

http://localhost:8000/blogs?page[limit]=1&page[offset]=1

Filters

I plan to introduce filters in a separate article.

Summary

I hope this serves as a helpful reference for using JSON:API.