tortoise-orm
tortoise-orm copied to clipboard
Ability to pass in Pydantic Config to pydantic_model_creator
Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Describe the solution you'd like
The ability to control the inner Config
class when generating Pydantic models (https://pydantic-docs.helpmanual.io/usage/model_config/)
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered. Cleaner than having to manually set config attributes afterwards
Additional context Add any other context about the feature request here.
Hi @lovetoburnswhen !
I am facing the problem mentioned in this issue and #1042 I am trying to achieve camelCase schema generation for FastAPI.
So far I have tried to manually set config attributes like below with no luck so far:
from humps import camelize
from src.db.models import Plan
def to_camel(field_name):
return camelize(field_name)
PdPlan = pydantic_model_creator(Plan, name="PdPlan")
setattr(PdPlan.Config, "alias_generator", to_camel)
setattr(PdPlan.Config, "allow_population_by_field_name", True)
PdPlan.schema()
It looks like fields are set. But .schema() method still display snake_case fields.
May I ask how you managed to set config attributes afterwards of model creation as you mentioned above?
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered. Cleaner than having to manually set config attributes afterwards
Edit: I ended up implementing something like this:
from humps import camelize
from pydantic import Base Model
from src.db.models import Plan
def to_camel(field_name):
return camelize(field_name)
__PdPlan = pydantic_model_creator(Plan, name="PdPlan")
class __PydanticConfig:
orm_mode = True
alias_generator = to_camel
allow_population_by_field_name= True
class PdPlan(__PdPlan):
class Config(__PydanticConfig):
pass
It's not perfect, but at least it solved my problem for this time. Anyone stuck with same problem may use until this #1048 arrives.