fastapi-sqlmodel-alembic icon indicating copy to clipboard operation
fastapi-sqlmodel-alembic copied to clipboard

Why is there a extra step to recreate objects?

Open bastianh opened this issue 3 years ago • 0 comments

I wonder if

@app.get("/songs", response_model=list[Song])
async def get_songs(session: AsyncSession = Depends(get_session)):
    result = await session.execute(select(Song))
    return result.scalars().all()

wouldn't be the same but shorter then

@app.get("/songs", response_model=list[Song])
async def get_songs(session: AsyncSession = Depends(get_session)):
    result = await session.execute(select(Song))
    songs = result.scalars().all()
    return [Song(name=song.name, artist=song.artist, year=song.year, id=song.id) for song in songs]

or is it necessary to create the new models for some reason?

bastianh avatar Jan 28 '22 13:01 bastianh