圆形进口
我有3个用于Pydantic型号的文件。在每个文件中,都有一个来自不同文件的模型的引用,该模型正在创建循环依赖性。我有这些循环依赖性,因为我想在API响应中嵌入数据,这可能最终是我的根源问题,但我相信目前我想实现的目标是可能的,我想知道如何实现。
我还尝试过将一些模型导入main.py.py
,但是现在这3个模型是如此相互依存的,我认为解决方案已经停止工作了,此外,它感觉有点hack虫,所以我没有喜欢这样做。
我在下面链接了一个类似的问题,但是这个问题的建议对我没有帮助。另外,我不知道两年前可能会有发展。
以下是我正在使用的简化模型。
# user models
class UserSchema(BaseSchema):
id: uuid.UUID
CheckinSchema = ForwardRef("CheckinSchema")
class UserSchemaDetails(UserSchema):
latest_checkin: Optional[CheckinSchema]
from app.schemas.checkin import CheckinSchema
UserSchemaDetails.update_forward_refs()
# checkin models
UserSchema = ForwardRef("UserSchema")
NakamalSchema = ForwardRef("NakamalSchema")
class CheckinSchema(CheckinSchemaBase):
id: uuid.UUID
user: UserSchema
nakamal: NakamalSchema
from app.schemas.user import UserSchema
from app.schemas.nakamal import NakamalSchema
CheckinSchema.update_forward_refs()
# nakamal models
UserSchema = ForwardRef("UserSchema")
class NakamalSchema(NakamalSchemaBase):
id: uuid.UUID
chief: Optional[UserSchema] = None
from app.schemas.user import UserSchema
NakamalSchema.update_forward_refs()
类似问题: 如何正确构建Pydantic Models?
I have 3 files for Pydantic models. In each file there is a reference to a model from a different file which is creating a circular dependency. I have these circular dependencies since I want to have embedded data in the API responses which maybe ultimately is my root issue but I believe what I want to achieve at the moment is possible and I want to know how.
I've also tried importing some of the models to the main.py
but now that these 3 models are so interdependent I think that solution has stopped working, besides it felt kinda hacky anyways so I didn't like doing that.
I linked a similar issue below, but the recommendations from that question are not helping me. Plus, there may be developments from 2 years ago that I'm not aware of.
Below are the simplified models I'm working with.
# user models
class UserSchema(BaseSchema):
id: uuid.UUID
CheckinSchema = ForwardRef("CheckinSchema")
class UserSchemaDetails(UserSchema):
latest_checkin: Optional[CheckinSchema]
from app.schemas.checkin import CheckinSchema
UserSchemaDetails.update_forward_refs()
# checkin models
UserSchema = ForwardRef("UserSchema")
NakamalSchema = ForwardRef("NakamalSchema")
class CheckinSchema(CheckinSchemaBase):
id: uuid.UUID
user: UserSchema
nakamal: NakamalSchema
from app.schemas.user import UserSchema
from app.schemas.nakamal import NakamalSchema
CheckinSchema.update_forward_refs()
# nakamal models
UserSchema = ForwardRef("UserSchema")
class NakamalSchema(NakamalSchemaBase):
id: uuid.UUID
chief: Optional[UserSchema] = None
from app.schemas.user import UserSchema
NakamalSchema.update_forward_refs()
Similar issue:
How to correctly structure pydantic models?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于这个问题的Pydantic和Fastapi Github存储库(仍然存在吗?)持续的问题。支持三种导入是不可能的,因为其他软件包,例如
棉花糖
或sqlalchemy
轻松处理这些情况。作为一个肮脏的解决方案,我必须在我正在努力避免进口的模块中进行最小的模式,并发表评论,以保持本地架构和原始的完整模式相互最新。
There was (are still?) ongoing issues on the Pydantic and FastAPI github repos about this very question. Supporting a 3 way import is not possible which I find frustrating since other packages such as
Marshmallow
orSQLAlchemy
handle these situation easily.As a dirty solution I had to make a minimal schema within the module I was working to avoid imports and leave a comment to keep both the local schema and the original full schema up-to-date with each other.