我有类似的pydantic模型
class AccountingQueueFilters(BaseModel):
no_batch_id: Optional[bool] = None
但是对于来自get params的输入'false'
,它返回True
https://mysite.tech/accounting_queue/get?no_batch_id=false
如何修复?
您应该使用这个构造它将允许您通过点符号访问属性,并进行自定义类型检查
class QueryParams:
def make_bool(self, value):
return not(value == "false") # some logic
def __init__(self, boolable: str, zero: bool):
self.boolable = self.make_bool(boolable)
self.zero = zero
@router.get('/route')
async def route(
data: QueryParams = Depends(),
):
print(data) # boolable=false : str
print(type(data.boolable)) # bool
print("success")
return Result(data)