-
questionversionfastapi: 0.63.0 # model
class Order(Model):
created_at = fields.DatetimeField(auto_now_add=True, null=False)
def created_at_(self):
return self.created_at.strftime('%Y-%m-%d %H:%M:%S')
# pydantic schema
class Schema(BaseModel):
created: str = Field(..., alias='created_at_')
class Config:
orm_mode = True
# api
@router.get('', response_model=Schema)
async def read():
Creator = pydantic_model_creator(Order, exclude=('created_at',), compused=('created_at_',))
query = await Order.filter(id=q_id).first()
order = Creator().from_tortoise_orm(query)
print(order)
# there is created_at_
print(Schema.from_orm(query))
# there is created
return orderbut the api return is {"created_at_": "..."} not {"created": "..."} and how to use alias |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
What is |
Beta Was this translation helpful? Give feedback.
-
from tortoise.contrib.pydantic import pydantic_model_creator, pydantic_queryset_creator
Creator = pydantic_model_creator(Order, exclude=exclude, compused=compused) |
Beta Was this translation helpful? Give feedback.
-
|
It seems like this would be a question for Tortoise, not for FastAPI, it's probably better to ask there.
|
Beta Was this translation helpful? Give feedback.
It seems like this would be a question for Tortoise, not for FastAPI, it's probably better to ask there.