Lullaby
Lullaby

Reputation: 21

Python FastAPI and Peewee ORM "select()" ERROR

I trying to to do a getAll controller for my Rest API

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

But it returns an error when I make a request.

The error:

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

And it is not a problem of the Note model because the other controllers work perfectly, for example this controller to update works correctly:

#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

Also i try to use "Note.select().get()" or "Note.select().where(True).get()" and still don't work :(

Here the full project: https://github.com/LucianoBrumer/python-fastapi-api-template

Thanks for reading!

Upvotes: 0

Views: 400

Answers (1)

coleifer
coleifer

Reputation: 26235

Try converting the query you're returning into a list of dicts:

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

If that doesn't work (I'm not too sure about the implementation of fast api's json encoder):

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

Upvotes: 1

Related Questions