如何响应FastAPI中的关系数据?
我正在使用FastApi进行后端,其中有两个模式
import datetime as dt
from typing import Optional
from pydantic import BaseModel, Field
class TradeDetails(BaseModel):
buySellIndicator: str = Field(description="A value of BUY for buys, SELL for sells.")
price: float = Field(description="The price of the Trade.")
quantity: int = Field(description="The amount of units traded.")
class Config:
orm_mode = True
class Trade(BaseModel):
asset_class: Optional[str] = Field(alias="assetClass", default=None,
description="The asset class of the instrument traded. E.g. Bond, Equity, FX...etc")
counterparty: Optional[str] = Field(default=None,
description="The counterparty the trade was executed with. May not always be available")
instrument_id: str = Field(alias="instrumentId",
description="The ISIN/ID of the instrument traded. E.g. TSLA, AAPL, AMZN...etc")
instrument_name: str = Field(alias="instrumentName", description="The name of the instrument traded.")
trade_date_time: dt.datetime = Field(alias="tradeDateTime", description="The date-time the Trade was executed")
trade_details: TradeDetails = Field(alias="tradeDetails",
description="The details of the trade, i.e. price, quantity")
trade_id: str = Field(alias="tradeId", default=None, description="The unique ID of the trade")
trader: str = Field(description="The name of the Trader")
class Config:
orm_mode = True
,对于这两个模式,我在数据库中创建了两个与关系的表,
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from database import Base
class TradeDetails(Base):
__tablename__ = "trade_details"
id = Column(Integer, primary_key=True, index=True)
buySellIndicator = Column(String)
price = Column(Float)
quantity = Column(Integer)
trades = relationship("Trade", back_populates='trade_details')
class Trade(Base):
__tablename__ = "trade"
trade_id = Column(String, primary_key=True, index=True)
trader = Column(String)
trade_details_id = Column(Integer, ForeignKey("trade_details.id"))
trade_details = relationship("TradeDetails", back_populates='trades')
asset_class = Column(String)
counterparty = Column(String)
instrument_id = Column(Integer)
instrument_name = Column(String)
trade_date_time = Column(DateTime)
现在我正在执行CRUD操作。
我正在犯的错误是什么,让我知道,我该如何解决此问题?
I am using fastAPI for backend in which there are two schemas
import datetime as dt
from typing import Optional
from pydantic import BaseModel, Field
class TradeDetails(BaseModel):
buySellIndicator: str = Field(description="A value of BUY for buys, SELL for sells.")
price: float = Field(description="The price of the Trade.")
quantity: int = Field(description="The amount of units traded.")
class Config:
orm_mode = True
class Trade(BaseModel):
asset_class: Optional[str] = Field(alias="assetClass", default=None,
description="The asset class of the instrument traded. E.g. Bond, Equity, FX...etc")
counterparty: Optional[str] = Field(default=None,
description="The counterparty the trade was executed with. May not always be available")
instrument_id: str = Field(alias="instrumentId",
description="The ISIN/ID of the instrument traded. E.g. TSLA, AAPL, AMZN...etc")
instrument_name: str = Field(alias="instrumentName", description="The name of the instrument traded.")
trade_date_time: dt.datetime = Field(alias="tradeDateTime", description="The date-time the Trade was executed")
trade_details: TradeDetails = Field(alias="tradeDetails",
description="The details of the trade, i.e. price, quantity")
trade_id: str = Field(alias="tradeId", default=None, description="The unique ID of the trade")
trader: str = Field(description="The name of the Trader")
class Config:
orm_mode = True
and for these two schemas I have created two tables in my database with relationships
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from database import Base
class TradeDetails(Base):
__tablename__ = "trade_details"
id = Column(Integer, primary_key=True, index=True)
buySellIndicator = Column(String)
price = Column(Float)
quantity = Column(Integer)
trades = relationship("Trade", back_populates='trade_details')
class Trade(Base):
__tablename__ = "trade"
trade_id = Column(String, primary_key=True, index=True)
trader = Column(String)
trade_details_id = Column(Integer, ForeignKey("trade_details.id"))
trade_details = relationship("TradeDetails", back_populates='trades')
asset_class = Column(String)
counterparty = Column(String)
instrument_id = Column(Integer)
instrument_name = Column(String)
trade_date_time = Column(DateTime)
Now I am performing CRUD operations.
When I am trying to create trade this is how request body looks like
But when I execute the post method to create trade response body don't return tradeDetails data which is nested in the trade model
What's the mistake I am doing let me know and how can I fix this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论