Overview
In the following article, I confirmed the basic operations of Django REST framework JSON:API (DJA).
In this article, I will try adding a custom model to DJA.
References
I will add a UserInfo model, referencing the following article.
https://tech-blog.rakus.co.jp/entry/20220329/python
Steps
Define the Model
Add the following:
# ユーザ情報を格納する
class UserInfo(BaseModel):
user_name = models.CharField(verbose_name='ユーザ名',max_length=32) # ユーザ名
birth_day = models.DateField(verbose_name='生年月日') # 生年月日
age = models.PositiveSmallIntegerField(verbose_name='年齢',null=True,unique=False) # 年齢
created_at = models.DateTimeField(verbose_name='作成日時',auto_now_add=True)
Build the Database
Execute the following:
% django-admin makemigrations --settings=example.settings
Migrations for 'example':
example/migrations/0013_userinfo.py
- Create model UserInfo
% django-admin migrate --settings=example.settings
Operations to perform:
Apply all migrations: auth, contenttypes, example, sessions, sites
Running migrations:
Applying example.0013_userinfo... OK
For reference, the following file is created:
# Generated by Django 4.1.8 on 2023-06-04 17:35
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("example", "0012_author_full_name"),
]
operations = [
migrations.CreateModel(
name="UserInfo",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("modified_at", models.DateTimeField(auto_now=True)),
("user_name", models.CharField(max_length=32, verbose_name="ユーザ名")),
("birth_day", models.DateField(verbose_name="生年月日")),
("age", models.PositiveSmallIntegerField(null=True, verbose_name="年齢")),
(
"created_at",
models.DateTimeField(auto_now_add=True, verbose_name="作成日時"),
),
],
options={
"abstract": False,
},
),
]
Create Components
Serializer
Add the following:
...
from example.models import (
...,
UserInfo
)
class UserInfoSerializer(serializers.ModelSerializer):
class Meta:
model = UserInfo
fields = ('id','user_name', 'birth_day','age','created_at')
class UserInfoDRFSerializer(drf_serilazers.ModelSerializer):
"""
DRF default serializer to test default DRF functionalities
"""
class Meta:
model = UserInfo
fields = ('id','user_name', 'birth_day','age','created_at')
...
ViewSets
Define the View class.
...
from example.models import (
...,
UserInfo
)
from example.serializers import (
...,
UserInfoSerializer
)
...
class UserInfoViewset(ModelViewSet):
queryset = UserInfo.objects.all()
serializer_class = UserInfoSerializer
def get_object(self):
entry_pk = self.kwargs.get("entry_pk", None)
if entry_pk is not None:
return Entry.objects.get(id=entry_pk).blog
return super().get_object()
...
Router
Add the following:
...
from example.views import (
AuthorRelationshipView,
...
UserInfoViewset
)
router = routers.DefaultRouter(trailing_slash=False)
router.register(r"blogs", BlogViewSet)
...
router.register(r"user-info", UserInfoViewset)
Results
Accessing http://localhost:8000/ shows that user-info has been created.

The list was also retrievable at http://localhost:8000/user-info.

Summary
We hope this serves as a useful reference for using DJA.