I have a model from my database in 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)
and a pydantic model in 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
I get data from the database which go through the ORM and in app.get() I declare the response model equal to List[schema.Something], but I want to change the names from the database a, b, c, d to more beautiful names.
Is there a solution like mapping names in NestJS?
Try to use aliases, this is what option allow_population_by_field_name allows to do, for example:
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'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With