kvo*_*the 4 orm sqlalchemy python-3.x pydantic fastapi
我的数据库中有一个模型models.py
:
class Something(Base):
__tablename__ = "something"
DATE = Column(Date, primary_key=True, index=True )
a = Column(String, primary_key=True, index=True)
b = Column(Integer, primary_key=True, index=True)
c = Column(Float, index=True)
d = Column(Integer, index=True)
e = Column(Integer, index=True)
f = Column(Float, index=True)
g = Column(Float, index=True)
h = Column(Integer, index=True)
Run Code Online (Sandbox Code Playgroud)
和一个 pydantic 模型schema.py
:
class Something(BaseModel):
DATE: date
a: str
b: int
c: float = None
d: int = None
e: int = None
f: float = None
g: float = None
h: int = None
class Config:
orm_mode = True
Run Code Online (Sandbox Code Playgroud)
我从通过 ORM 的数据库获取数据,并app.get()
声明响应模型等于List[schema.Something]
,但我想将数据库中的名称更改a, b, c, d
为更漂亮的名称。NestJS 中有映射名称之类的解决方案吗?
尝试使用别名,这是选项allow_population_by_field_name
允许执行的操作,例如:
class Person(BaseModel):
first_name: str = Field(..., alias='first')
last_name: str = Field(..., alias='second')
class Config:
allow_population_by_field_name = True
p = Person(**{'first': 'John', 'second': 'White'})
print(p.__dict__)
# {'first_name': 'John', 'last_name': 'White'}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3493 次 |
最近记录: |