如何响应FastAPI中的关系数据?

发布于 2025-02-12 12:09:47 字数 3222 浏览 0 评论 0原文

我正在使用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
enter image description here

But when I execute the post method to create trade response body don't return tradeDetails data which is nested in the trade model
enter image description here

The method I am using to post
enter image description here

What's the mistake I am doing let me know and how can I fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文