[Python] working with fastapi i cannot figure out how json works and it throws error

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    so I am trying to execute this code so that i can get what is the result of it:

    @router.get("")
    def get_post(db: Session = Depends(get_session), limit: int = 10, skip: int = 0, search: str = ''):
    # posts = db.query(Posts).filter(Posts.title.contains(search)).limit(limit).offset(skip).all()

    posts_joined = db.query(Posts, func.count(Votes.post_id).label("votes"))
    .join(Votes, Votes.post_id == Posts.post_id, isouter=True).group_by(Posts.post_id).all()

    return posts_joined


    however, this throws some kind of error. actually i am trying to copy what this guy is doing in his video (on 10:21:13) in order to see json like this

    [
    {
    "post":{
    "title": something,
    "content": something,
    (and so on)
    },
    "votes": 1
    },
    {
    "post":{
    "title": something1,
    "content": something1,
    (and so on)
    },
    "votes": 0
    }
    ]


    as i have tried to figure out with AI myself, they tell me that its because this posts_joined returns tuple of post and vote and it cannot translate it into json by itself and offers some kind of hardcoded version to create loop. however since that guy can do that easily just like i write why cannot i do that. could you help me why does that version from the video works and not my version? is this because of modules versions or something?

    Continue reading...

Compartilhe esta Página