惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
B
Blog
腾讯CDC
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
L
LangChain Blog
F
Fortinet All Blogs
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog

Python

[开源] CNEquity:自托管 A 股 Parquet 数据湖(日更 / PIT / 防幸存者偏差 / MCP) - V2EX 没人觉得 cursor 的模型思考很慢嘛,经常思考卡死要手动停止再继续 - V2EX [原创工具] hot-list 多平台热榜聚合+AI 分析系统,一键部署,开源免费 - V2EX 开发了一个查看 conda 镜像工作情况的项目,已经开源 - V2EX JD 评论风控问题 - V2EX [开源分享] QQ 空间 Flash 老游戏-红警大战坦克风暴-保活与每日脚本 - V2EX TG api 我用 google Voice 老申请失败 哪位朋友帮忙用 mac 测试下我的 Python 脚本 用 GPT5.6 填了之前的坑:在线的 Python 编辑器和运行终端 为什么这段 cuda 的并行前缀算法不会数据竞争 怎么搞定纯 Python 代码解码 jpg 图片,要求无外部依赖 使用 kkRepo 搭建 Python PyPI 私服 Python WebSocket 长连接到底怎么写才稳? 小白求推荐人工智能学习的网课 humanize-text 一个开源的 AI 文本拟人化工具集 9.9 元起!跨境卖家疯抢的纯净住宅 IP 辣椒 HTTP,到底有多香? 从会议录音到知识库全自动:我把数百段录音做成了可 RAG 问答的 Wiki(附开源代码) CodexSaver:在不让 Codex 变笨的前提下,让它更便宜。 [开源] 我正在用 Python 复刻经典游戏红色警戒 2 有准确绘制缠论中枢的 Python 代码借鉴么 做了一个面向张量计算的语言,可从 Python 调用,支持显式索引和自动求导 把电脑伪装成电视,用 DLNA 投屏拿到视频号直播流地址 爬虫开发工作中,你们是如何基于 AI 进行提效的? 发现 Python 一个有意思的小特性,发现很合适搞成面试题。问了 AI 都不行:),欢迎来挑战~ 大型 Python 开源项目都不会对变量进行类型注解? 使用 pycharm 开发 Python ,自定义代码风格,并实时提示 创造了 uv 的 Astral 公司被 OpenAI 收购 慎用 PyCharm Remote Development 功能 Python 3.15 JIT 的最新进展,已经有大概 5%的性能提升了 [开源] 做了个 feishu-docx,把飞书知识库变成 AI 更容易读写和管理的内容源,方便给 Agent 用
小白求教个循环导入的问题
dylyft · 2025-11-10 · via Python

最近刚开始学 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