Skip to content

Overriding Pydantic Configuration

For a Django Ninja Schema, there are many customization settings available via the schema's Pydantic Config class.

Note

Under the hood, Django Ninja uses Pydantic Models and all their features and benefits. The alias Schema was chosen to avoid confusion when working with Django models in code, because Pydantic's model class is called "Model" by default, which conflicts with Django's "Model" class.

Example CamelCase Schema

An interesting Config attribute is the alias_generator. An example using Pydantic in Django Ninja might look like this:

from ninja import Schema


def to_camel(string: str) -> str:
    return ''.join(word.capitalize() for word in string.split('_'))


class CamelModelSchema(Schema):
    str_field_name: str
    float_field_name: float

    class Config(Schema.Config):
        alias_generator = to_camel

Note

When overriding the schema's Config, it is necessary to inherit from the base Config class.

Keep in mind that when you want to modify the output of field names (such as camelCase) — you also need to set populate_by_name and by_alias

class UserSchema(ModelSchema):
    class Config:
        model = User
        model_fields = ["id", "email"]
        alias_generator = to_camel
        populate_by_name = True  # !!!!!! <--------


@api.get("/users", response=list[UserSchema], by_alias=True) # !!!!!! <-------- by_alias
def get_users(request):
    return User.objects.all()

Result:

[
  {
    "Id": 1,
    "Email": "tim@apple.com"
  },
  {
    "Id": 2,
    "Email": "sarah@smith.com"
  }
  ...
]

Custom Configuration from Django Models

When using create_schema, the generated schema can be used to build another class with custom configuration, such as:

from django.contrib.auth.models import User
from ninja.orm import create_schema


BaseUserSchema = create_schema(User)


class UserSchema(BaseUserSchema):

    class Config(BaseUserSchema.Config):
        ...

Done

Continue to the next chapter! Pagination

Comments


本文总阅读量次