diff --git a/backend/common/schema.py b/backend/common/schema.py index b64da0b2..1e0d3400 100644 --- a/backend/common/schema.py +++ b/backend/common/schema.py @@ -37,8 +37,10 @@ class SchemaBase(BaseModel): # 详情:https://fastapi-practices.github.io/fastapi_best_architecture_docs/backend/reference/pk.html#%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9 @field_serializer('id', check_fields=False) - def serialize_id(self, value: int) -> str: - return str(value) + def serialize_id(self, value: int) -> str | int: + if self.model_config.get('from_attributes'): + return str(value) + return value def ser_string(value: Any) -> str | None: