根据字段的值使用不同的 pydantic 模型

Using different pydantic models depending on the value of the fields

我有 2 个 pydantic 模型(var1 和 var2)。 PostExample 方法的输入可以接收第一个模型或第二个模型的数据。 Union 的使用有助于解决这个问题,但在验证期间它会为第一个模型和第二个模型抛出错误。

如何做到在填写字段时出错时,只返回特定模型的验证器错误,而不是同时返回两者? (如果有帮助,那么模型可以通过字段A的长度来区分)

main.py

@app.post("/PostExample")
def postExample(request: Union[schemas.var1, schemas.var2]):
    
    result = post_registration_request.requsest_response()
    return result
  
  

schemas.py

class var1(BaseModel):
    A: str
    B: int
    C: str
    D: str
  
  
class var2(BaseModel):
    A: str
    E: int
    F: str

您可以使用 Discriminated Unions(感谢@larsks 在评论中提到了这一点)。设置一个可区分的联合,“验证速度更快,因为它只针对一个模型进行尝试”,以及“在失败的情况下只引发一个显式错误”。 =30=]。下面的工作示例:

app.py

import schemas
from fastapi import FastAPI, Body
from typing import Union

app = FastAPI()

@app.post("/")
def submit(item: Union[schemas.Model1, schemas.Model2] = Body(..., discriminator='model_type')):
    return item

schemas.py

from typing import Literal
from pydantic import BaseModel

class Model1(BaseModel):
    model_type: Literal['m1']
    A: str
    B: int
    C: str
    D: str
  
class Model2(BaseModel):
    model_type: Literal['m2']
    A: str
    E: int
    F: str

测试输入-输出

#1 Successful Response   #2 Validation error                   #3 Validation error
                                          
# Request body           # Request body                        # Request body
{                        {                                     {
  "model_type": "m1",      "model_type": "m1",                   "model_type": "m2",
  "A": "string",           "A": "string",                        "A": "string",
  "B": 0,                  "C": "string",                        "C": "string",
  "C": "string",           "D": "string"                         "D": "string"
  "D": "string"          }                                     }
}                                                              
                        
# Server response        # Server response                     # Server response
200                      {                                     {
                           "detail": [                           "detail": [
                             {                                     {
                               "loc": [                              "loc": [
                                 "body",                               "body",
                                 "Model1",                             "Model2",
                                 "B"                                   "E"
                               ],                                    ],
                               "msg": "field required",              "msg": "field required",
                               "type": "value_error.missing"         "type": "value_error.missing"
                             }                                     },
                           ]                                       {
                         }                                           "loc": [
                                                                       "body",
                                                                       "Model2",
                                                                       "F"
                                                                     ],
                                                                     "msg": "field required",
                                                                     "type": "value_error.missing"
                                                                   }
                                                                 ]
                                                               }

另一种方法是尝试解析模型(基于您作为 query/path 参数传递的鉴别器),如 .

所述