NinjaAPI and orjson

Hello everyone,

I was researching how to use orjson and ninja api features. I want to share the sample code below. Is my ORJSONRenderer correct? Do I need to pass data to the render function inside this class? I’m a bit new and need info and couldn’t find any sample code. I wanted to use a fast api like ninja/orjson because there will be a lot of json data in the app I will code. Thank you in advance.

schemas.py

from ninja import Schema, ModelSchema
from App.models import Person

class PersonOut(ModelSchema):
    full_name: str

    class Config:
        model = Person
        model_fields = ["id", "birth_year", ]

    @staticmethod
    def resolve_full_name(obj):
        return f"{obj.name}, {obj.title}"

models.py

from django.db import models

# Create your models here.

class Person(models.Model):
    name = models.CharField(max_length=50)
    title = models.CharField(max_length=100)
    birth_year = models.PositiveSmallIntegerField()

api.py

from ninja import Router
from App.schemas import PersonOut
from App.models import Person

router = Router()

@router.get("/home", response=list[PersonOut])
def home(request):
    return Person.objects.all()

urls.py

from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI
from ninja.renderers import BaseRenderer
import orjson

from App.api import router as App_router

class ORJSONRenderer(BaseRenderer):
    def render(self, request, data, *, response_status):
        return orjson.dumps(data)

api = NinjaAPI(renderer=ORJSONRenderer())
api.add_router("/App/", App_router)

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

json

[
   {
      "id":1,
      "birth_year":100,
      "full_name":"Cardinal, IT"
   },
   {
      "id":2,
      "birth_year":200,
      "full_name":"Priest, Cold"
   },
   {
      "id":3,
      "birth_year":300,
      "full_name":"People, Hot"
   }
]

I mean, how can I return the QuerySet data I have obtained with Music.objects.all() in orjson format.

Or am I already doing this in the code above? The data I returned is as follows:

[{“name”:“Knock Knock”,“owner”:{“id”:1,“birth_year”:100,“full_name”:“Cardinal, IT”}}]