【发布时间】:2022-03-20 03:18:49
【问题描述】:
Pydantic says,您可以通过简单地定义__get_validators__ 方法来创建自定义类。如果您想解析为具有自己的元类的类或出于其他原因不想从 BaseModel 继承,这很有用。
但是,这在 FastAPI 中奇怪的地方失败了。例如,FastAPI 不会将这样的类检测为 body 参数,而是始终认为它是查询参数。
from fastapi import FastAPI, Body
from fastapi.testclient import TestClient
app = FastAPI()
class NastyMetaClass(type):
pass
class Foo(metaclass=NastyMetaClass):
@classmethod
def __get_validators__(cls):
yield lambda value: True
@app.post("/implicit")
def foo(foo: Foo): # This is supposed to work, but does not
return "It worked"
@app.post("/explicit")
def foo_body(foo: Foo = Body(...)): # The `= Body(...)` fixes it
return "It worked"
client = TestClient(app)
response = client.post("/implicit", json={})
print(response.json())
# {'detail': [{'loc': ['query', 'foo'], 'msg': 'field required', 'type': 'value_error.missing'}]}
response = client.post("/explicit", json={})
print(response.json())
# It worked
如何让 FastAPI 识别自定义 Pydantic 类?
【问题讨论】: