Pydantic 是否接受具有单值和多值的相同查询?
Does Pydantic accept the same query with both single and multi values?
我的架构:
class ArticleBase(Schema):
page: Optional[int] = 1
topic: Optional[List[str]] = Query(None)
路由器:
@api.get("/articles/", tags = ['Articles'])
def article_list(request, article: ArticleBase = Query(...)):
return article
我希望 Pydantic/FastAPI 接受单值和多值查询。
我希望他们都被录取:
www.example.com/articles/?topic=hello&topic=world
www.example.com/articles/?topic=hello
如何实现?
我收到的错误消息:
"msg": "value is not a valid list",
"type": "type_error.list"
通过从我的代码中删除 Optional
,我让它按我想要的方式工作。它接受具有单个值和多个值的查询。
我的工作代码:
class ArticleBase(Schema):
page: Optional[int] = 1
topic: List[str] = Query(None)
不明白这是为什么。也许我遗漏了文档中的某些内容,但仔细阅读并没有找到任何相关内容。
我的架构:
class ArticleBase(Schema):
page: Optional[int] = 1
topic: Optional[List[str]] = Query(None)
路由器:
@api.get("/articles/", tags = ['Articles'])
def article_list(request, article: ArticleBase = Query(...)):
return article
我希望 Pydantic/FastAPI 接受单值和多值查询。
我希望他们都被录取:
www.example.com/articles/?topic=hello&topic=world
www.example.com/articles/?topic=hello
如何实现?
我收到的错误消息:
"msg": "value is not a valid list",
"type": "type_error.list"
通过从我的代码中删除 Optional
,我让它按我想要的方式工作。它接受具有单个值和多个值的查询。
我的工作代码:
class ArticleBase(Schema):
page: Optional[int] = 1
topic: List[str] = Query(None)
不明白这是为什么。也许我遗漏了文档中的某些内容,但仔细阅读并没有找到任何相关内容。