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

由网友(缺心眼)分享简介:我有两个简单的模型(var1和var2)。PostExample方法的输入可以接收第一个模型或第二个模型的数据。使用Union有助于解决此问题,但在验证过程中,它会为第一个模型和第二个模型抛出错误。如何使其在填写字段时出错的情况下,只为某个模型返回验证器错误,而不是同时为两个模型返回?(如果有帮助,则可以通过字段A...

我有两个简单的模型(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
  
  
sql如何根据一个字段的多个值查询

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在评论中提到了这一点)。设置区分联合,&q;验证速度更快,因为它只尝试针对一个模型&,并且在失败的情况下只会引发一个显式错误&。工作示例如下:

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"
                                                                   }
                                                                 ]
                                                               }

另一种方法是尝试解析模型(基于作为查询/路径参数传递的鉴别符),如here (Update 1)所述。

阅读全文

相关推荐

最新文章