我想在我的FastAPI模型中使用枚举字段。这是我的模型:
import enum
from sqlalchemy import Column, Enum, Integer, String
class Gender(enum.Enum):
FEMALE = "FEMALE"
MALE = "MALE"
...
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)
gender Column(Enum(Gender), default=Gender.FEMALE)
以下是我的架构:
from enum import Enum
from pydantic import BaseModel
class Gender(str, Enum):
FEMALE = 'FEMALE'
MALE = 'MALE'
...
class User(BaseModel):
id: int
name: str
gender: Gender
class Config:
orm_mode = True
我收到了错误信息:
value is not a valid enumeration member; permitted: 'FEMALE', 'MALE' (type=type_error.enum; enum_values=[<Gender.FEMALE: 'FEMALE'>, <Gender.MALE: 'MALE'>])
。
更新:
现在我收到了错误信息:pydantic.error_wrappers.ValidationError: IdentificationRequestBase 响应的1个验证错误-> 0 -> 性别字段必需 (type=value_error.missing)
。