You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在FastAPI的Strawberry输入模式中排除继承字段?

在Strawberry中继承输入类时排除特定字段

问题场景

我正在开发一个基于FastAPI和Strawberry的GraphQL项目,需要从基础输入模式继承,但希望在派生模式中排除特定字段。当前代码如下:

import strawberry
from typing import Optional

@strawberry.input
class GenericRequest:
    country: Optional[str] = strawberry.field(default=None, description="Country where the lanes are located")
    segment_id: Optional[str] = strawberry.field(default=None, description="Unique identifier for the segment")

@strawberry.input
class LaneGraphRequest(GenericRequest):
    lane_graph_id: Optional[str] = strawberry.field(default=None, description="Id of a respective lane graph")
    
    # 我想从此模式中排除'country'字段

尝试重新定义country字段并设置exclude=True时触发了TypeError,仅不在派生类中声明该字段也无法移除继承的字段,请问在Strawberry中继承基础模式时,有没有推荐的方法排除特定字段?

解决方案

方法1:使用strawberry.field(skip=True)覆盖字段

在Strawberry 0.180.0及以上版本中,可在派生类中重新定义需要排除的字段,通过skip=True参数标记该字段不纳入GraphQL模式:

import strawberry
from typing import Optional

@strawberry.input
class GenericRequest:
    country: Optional[str] = strawberry.field(default=None, description="Country where the lanes are located")
    segment_id: Optional[str] = strawberry.field(default=None, description="Unique identifier for the segment")

@strawberry.input
class LaneGraphRequest(GenericRequest):
    lane_graph_id: Optional[str] = strawberry.field(default=None, description="Id of a respective lane graph")
    # 覆盖country字段并标记为跳过
    country: Optional[str] = strawberry.field(skip=True)

方法2:手动修改Strawberry定义移除字段

若使用较低版本的Strawberry,可直接操作类的内部定义结构来移除目标字段:

import strawberry
from typing import Optional

@strawberry.input
class GenericRequest:
    country: Optional[str] = strawberry.field(default=None, description="Country where the lanes are located")
    segment_id: Optional[str] = strawberry.field(default=None, description="Unique identifier for the segment")

@strawberry.input
class LaneGraphRequest(GenericRequest):
    lane_graph_id: Optional[str] = strawberry.field(default=None, description="Id of a respective lane graph")

# 过滤并移除country字段
LaneGraphRequest.__strawberry_definition__.fields = [
    field for field in LaneGraphRequest.__strawberry_definition__.fields 
    if field.name != "country"
]

方法3:结合Pydantic的字段排除功能

如果项目已使用Pydantic,可借助Strawberry的Pydantic集成,通过Pydantic的配置实现字段排除:

import strawberry
from pydantic import BaseModel, Field
from typing import Optional
from strawberry.experimental.pydantic import input

class GenericRequestModel(BaseModel):
    country: Optional[str] = Field(default=None, description="Country where the lanes are located")
    segment_id: Optional[str] = Field(default=None, description="Unique identifier for the segment")

class LaneGraphRequestModel(GenericRequestModel):
    lane_graph_id: Optional[str] = Field(default=None, description="Id of a respective lane graph")
    
    class Config:
        fields = {"country": {"exclude": True}}

@input(model=LaneGraphRequestModel)
class LaneGraphRequest:
    pass

@input(model=GenericRequestModel)
class GenericRequest:
    pass

内容的提问来源于stack exchange,提问作者Vinuta Hiremath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:22:48