1

我在尝试接受迂腐模型时遇到此错误。经过一段时间的调试后,我认为问题在于接受CodeCreate

Pydantic 模型

class BaseCode(BaseModel):
    index: Optional[int] = Field(None)
    email: EmailStr
    gen_time: datetime
    expire_time: datetime


class CodeCreate(BaseCode):
    code: int
    used_time: Optional[datetime] = Field(None)

    class Config:
        orm_mode = True

class Message(BaseModel):
    message: str

代码 ORM

class Code(Base):
    __tablename__ = 'code'

    index = Column(Integer, primary_key=True, autoincrement=True)
    code = Column(Integer)
    email = Column(String, ForeignKey('user.email'))
    gen_time = Column(DateTime)
    expire_time = Column(DateTime)
    used_time = Column(DateTime, nullable=True)

处理程序

@app.post('/verify-code', response_model=schemas.Message, responses={404: {"model": schemas.Message}, 406: {"model": schemas.Message}})
async def verify_code(code: schemas.CodeCreate, response: Response, device_name: str = Body(..., embed=True), db=Depends(get_db)):

    result = crud.verify_and_insert_code(db=db, code=code)
    if result == 'matched':
        response.status_code = status.HTTP_202_ACCEPTED
        return crud.start_new_session(db=db, session=schemas.Session(session_id='1234', start_time=datetime.now(), email=code.email, device_name=device_name))
    elif result == 'not-matched':
        response.status_code = status.HTTP_200_OK
    elif result == 'expire':
        response.status_code = status.HTTP_406_NOT_ACCEPTABLE
    elif result == 'invalid':
        response.status_code = status.HTTP_404_NOT_FOUND

    return schemas.Message(message="Item not found")

来自客户端的请求正文

{
  "code": {
    "index": 0,
    "email": "user@example.com",
    "gen_time": "2022-01-24T16:38:12.612Z",
    "expire_time": "2022-01-24T16:38:12.612Z",
    "code": 0,
    "used_time": "2022-01-24T16:38:12.612Z"
  },
  "device_name": "string"
}

422 的响应正文

{
  "detail": [
    {
      "loc": [
        "body",
        "code",
        "email"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [
        "body",
        "code",
        "gen_time"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [
        "body",
        "code",
        "expire_time"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    },
    {
      "loc": [
        "body",
        "code",
        "code"
      ],
      "msg": "field required",
      "type": "value_error.missing"
    }
  ]
}

临时解决方案

从 Pydantic 模型中删除这个 ORM 确认代码解决了这个问题。CodeCreate我相信pydantic 模型和ORM 模型之间可能存在冲突Code,但我不知道如何解决它。

class Config:
    orm_mode = True
4

3 回答 3

1

根据 此处的MDN ,a422 Unprocessable Entity表示无法处理请求的信息。

在这种情况下,最可能的问题是POST发送的请求的数据与 Pydantic 模型不匹配。

确保发送的数据格式正确。

于 2022-01-24T05:22:13.990 回答
0

正如上面 Brian Law 所说,您的请求正文是 的形式Code,它不是一个 pydantic 模型,而是一个数据库模型。

当您发送POST请求时,正文应与 匹配CodeCreate,而不是Code

于 2022-01-24T23:17:46.473 回答
0

调试了半天找到了解决办法。

配置的ORM迂腐模型不能用于接收来自客户端的请求。

就我而言,我必须创建另一个类来扩展CodeCreate该类,将ORM配置添加到该类并CodeCreate用于客户端的主体。

class BaseCode(BaseModel):
    index: Optional[int] = Field(None)
    email: EmailStr
    gen_time: datetime
    expire_time: datetime


class CodeCreate(BaseCode):
    code: int
    used_time: Optional[datetime] = Field(None)

    
class Code(CodeCreate):

    class Config:
            orm_mode = True

发布请求

@app.post('/verify-code')
async def verify_code(code: schemas.CodeCreate):
    return 'success'
于 2022-01-25T07:51:37.053 回答