如何使用 fastAPI 显示来自 SqlAlchemy Query 的列表数据
How to display list data from SqlAlchemy Query using fastAPI
所以,我一直在尝试学习 Python 使用 fastAPI 的全栈 Web 开发。我的大部分网站都运行正常,但我在使用 FASTAPI 发送查询列表时遇到问题。
我收到这个错误。
{
"detail": [
{
"loc": [
"path",
"id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
这是我的 FastAPI 路由器代码。
@router.get("/get/all", response_model=List[ComicViewable])
def list_of_all_comics(db: Session = Depends(get_db)):
comics = comic_func.list_comics(db)
if not comics:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="No Comics have been created.")
return comics
这是return数据库中存储的所有漫画列表的函数。
def list_comics(db: Session):
return db.query(Comic).all()
和我的傲慢回应class
class ComicViewable(BaseModel):
title: str
description: str
date_created: date
is_ongoing: bool
is_public: bool
class Config:
orm_mode = True
我不知道我做错了什么。我已经阅读了 FASTAPI 帮助文档,在那里他们还使用 List[PydanticClassName]
到 return 数据库查询列表,但由于某种原因,它对我不起作用。
编辑:
所以,我检查了 list_comics()
函数的响应。它确实 return 做出了正确的反应。我通过在 Jinja2 模板中打印 comic.title
来测试它。
{% block content %}
<div class="ui segment">
<div class="two column middle aligned grid">
{% for comic in comics %}
<h2>{{comic.title}}</h2>
{% endfor %}
</div>
</div>
{% endblock %}
所以错误似乎发生在 pydantic class 模型 或 FastAPI 路由器 .
@MatsLindh 建议的这个解决方案解决了它。
That is not the endpoint being loaded; the error message complains about an endpoint with id set (i.e. /get/{id} or something similar). You probably have that endpoint defined before your /get/all endpoint, and since all is not an integer, it throws the given error.
这解决了我的问题,因为我在名为 /get/{id}
的 /get/all
端点之前有另一个端点导致了错误。
所以,我一直在尝试学习 Python 使用 fastAPI 的全栈 Web 开发。我的大部分网站都运行正常,但我在使用 FASTAPI 发送查询列表时遇到问题。
我收到这个错误。
{
"detail": [
{
"loc": [
"path",
"id"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
这是我的 FastAPI 路由器代码。
@router.get("/get/all", response_model=List[ComicViewable])
def list_of_all_comics(db: Session = Depends(get_db)):
comics = comic_func.list_comics(db)
if not comics:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="No Comics have been created.")
return comics
这是return数据库中存储的所有漫画列表的函数。
def list_comics(db: Session):
return db.query(Comic).all()
和我的傲慢回应class
class ComicViewable(BaseModel):
title: str
description: str
date_created: date
is_ongoing: bool
is_public: bool
class Config:
orm_mode = True
我不知道我做错了什么。我已经阅读了 FASTAPI 帮助文档,在那里他们还使用 List[PydanticClassName]
到 return 数据库查询列表,但由于某种原因,它对我不起作用。
编辑:
所以,我检查了 list_comics()
函数的响应。它确实 return 做出了正确的反应。我通过在 Jinja2 模板中打印 comic.title
来测试它。
{% block content %}
<div class="ui segment">
<div class="two column middle aligned grid">
{% for comic in comics %}
<h2>{{comic.title}}</h2>
{% endfor %}
</div>
</div>
{% endblock %}
所以错误似乎发生在 pydantic class 模型 或 FastAPI 路由器 .
@MatsLindh 建议的这个解决方案解决了它。
That is not the endpoint being loaded; the error message complains about an endpoint with id set (i.e. /get/{id} or something similar). You probably have that endpoint defined before your /get/all endpoint, and since all is not an integer, it throws the given error.
这解决了我的问题,因为我在名为 /get/{id}
的 /get/all
端点之前有另一个端点导致了错误。