django-ninja
django-ninja copied to clipboard
Use `class Meta` or `class Config`
So the documentation states for ModelSchema
we should define it like
from django.contrib.auth.models import User
from ninja import ModelSchema
class UserSchema(ModelSchema):
class Meta:
model = User
fields = ['id', 'username', 'first_name', 'last_name']
However, I am converting everything in the response to camelCase for my API endpoints.
The documentation specified we should override pyandtic configs with the class Config
class UserSchema(ModelSchema):
class Config:
model = User
model_fields = ["id", "email"]
alias_generator = to_camel
populate_by_name = True # !!!!!! <------
Which one are we supposed to use? I would like to have consistency across my codebase.
Can we also use class Config
instead of class Meta:
for any Ninja Schema?
ALso by the way thank you so much for developing this library I absolutely love it? The best way to build a web framework using python.
Love fast API syntax and Django ORM, and this lib allows you to have both : )
As I understand class Config
is Pydantic v1 way of defining config for a BaseModel. But to be more "Django way of doing things" Django Ninja provides option to use also class Meta
. In Pydantic v2 you can also specify config directly on BaseModel/Schema with model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)
.
UPDATE: Actually I tested it and you must use class Meta
with ModelSchema because you must provide model and one of the fields/exclude options. But you can use model_config instead class Config for configuration settings.