繁体   English   中英

Python FastAPI 和 Peewee ORM “select()”错误

[英]Python FastAPI and Peewee ORM "select()" ERROR

我试图为我的 Rest API 做一个 getAll controller

@app.get("/api/note")
async def getAll():
    return Note.select()

但是当我发出请求时它返回一个错误。

错误:

File "C:\Users\Luciano\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\encoders.py", line 161, in jsonable_encoder
    return jsonable_encoder(
  File "C:\Users\Luciano\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\encoders.py", line 117, in jsonable_encoder
    encoded_value = jsonable_encoder(
  File "C:\Users\Luciano\AppData\Local\Programs\Python\Python310\lib\site-packages\fastapi\encoders.py", line 148, in jsonable_encoder
    if isinstance(obj, classes_tuple):
  File "C:\Users\Luciano\AppData\Local\Programs\Python\Python310\lib\abc.py", line 119, in __instancecheck__
    return _abc_instancecheck(cls, instance)
RecursionError: maximum recursion depth exceeded in comparison

这不是 Note model 的问题,因为其他控制器工作正常,例如这个 controller 更新工作正常:

#this works perfectly
@app.put("/api/note/{id}")
async def updateByID(note: NoteSchema, id):
    data = {
        "id": id,
        "title": note.title,
        "description": note.description
    }
    Note.update({
        Note.title: data.get('title'),
        Note.description: data.get('description'),
    }).where(Note.id == id).execute()

    return data

我也尝试使用“Note.select().get()”或“Note.select().where(True).get()”但仍然不起作用:(

这里是完整的项目: https://github.com/LucianoBrumer/python-fastapi-api-template

谢谢阅读!

尝试将您返回的查询转换为字典列表:

@app.get("/api/note")
async def getAll():
    return Note.select().dicts()

如果这不起作用(我不太确定快速 api 的 json 编码器的实现):

@app.get("/api/note")
async def getAll():
    return list(Note.select().dicts())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM