小白求教个循环导入的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dylyft
V2EX    Python

小白求教个循环导入的问题

  •  
  •   dylyft 4 小时 5 分钟前 237 次点击

    最近刚开始学 python 和 fastapi, 按照 fastapi 教程学习时遇到个问题, 因为使用 sqlmodel 的 relationship, 导致两个模型文件相互导入, 然后报错了, 根据官方文档的解决办法, 使用 TYPE_CHECKING 和字符串版本类型后暂时解决了报错.
    但是当接口通过 response_model 指定了数据模型(非表模型)作为返回类型时, 又出现了报错, 报错提示如下:

    `TypeAdapter[typing.Annotated[app.models.teams.TeamPublicWithUser, FieldInfo(annotation=TeamPublicWithUser, required=True)]]` is not fully defined; you should define `typing.Annotated[app.models.teams.TeamPublicWithUser, FieldInfo(annotation=TeamPublicWithUser, required=True)]` and all referenced types, then call `.rebuild()` on the instance. 

    求各位大佬帮忙看看, 哪里有问题. python 版本用的是 3.12, 具体代码如下
    models/users.py

    from typing import TYPE_CHECKING, Any, Optional from sqlmodel import Field, Relationship, SQLModel if TYPE_CHECKING: from .teams import Team, TeamPublic class UserBase(SQLModel): username: str = Field(index=True, max_length=255, unique=True) email: str | NOne= Field(default=None, index=True, max_length=255) is_active: bool = True is_superuser: bool = False full_name: str | NOne= Field(default=None, max_length=255) team_id: int | NOne= Field(default=None, foreign_key="team.id") class User(UserBase, table=True): id: int | NOne= Field(default=None, primary_key=True) hashed_password: str team: Optional["Team"] = Relationship(back_populates="members") class UserPublic(UserBase): id: int class UserPublicWithTeam(UserPublic): team: Optional["TeamPublic"] = None 

    models/teams.py

    from typing import TYPE_CHECKING, Any, List from sqlmodel import Field, Relationship, SQLModel if TYPE_CHECKING: from .users import User, UserPublic class TeamBase(SQLModel): name: str = Field(max_length=255) class Team(TeamBase, table=True): id: int | NOne= Field(default=None, primary_key=True) members: List["User"] = Relationship(back_populates="team") class TeamPublic(TeamBase): id: int class TeamPublicWithUser(TeamPublic): members: List["UserPublic"] = [] 

    router/users.py

    # 用户相关接口 from sqlmodel import and_, select from app.api.deps import SessionDep from app.models.users import ( User, UserPublicWithTeam, ) from app.new_router import new_router router = new_router(prefix="/users", tags=["用户管理"]) @router.get("/{id}", summary="获取用户详情", response_model=UserPublicWithTeam) async def get_user_api(db: SessionDep, id: int): user = db.exec(select(User).where(and_(User.id == id, User.is_active))).first() return user 

    router/teams.py

    # 团队相关接口 from sqlmodel import select from app.api.deps import SessionDep from app.models.teams import ( Team, TeamPublicWithUser, ) from app.new_router import new_router router = new_router(prefix="/teams", tags=["团队管理"]) @router.get("/{id}", summary="获取团队详情", response_model=TeamPublicWithUser) async def get_team_api(db: SessionDep, id: int): team = db.exec(select(Team).where(Team.id == id)).first() return team 
    目前尚无回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5392 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 70ms UTC 07:30 PVG 15:30 LAX 23:30 JFK 02:30
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86