Skip to content

Commit 008cd2c

Browse files
committed
add response models to the routes
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
1 parent a0885ce commit 008cd2c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from db import get_db, init_models
88
from fastapi import Depends, FastAPI
99
from models import User
10+
from schema import UserResponseModel
1011
from sqlalchemy import select
1112
from sqlalchemy.ext.asyncio import AsyncSession
1213

@@ -27,7 +28,7 @@ async def root() -> dict[str, str]:
2728
return {"message": "Test API for FastAPI and Async SQLAlchemy ."}
2829

2930

30-
@app.post("/users/")
31+
@app.post("/users/", response_model=UserResponseModel)
3132
async def create_user(
3233
name: str, email: str, session: AsyncSession = Depends(get_db)
3334
) -> User:
@@ -37,7 +38,7 @@ async def create_user(
3738
return user
3839

3940

40-
@app.get("/users/")
41+
@app.get("/users/", response_model=Sequence[UserResponseModel])
4142
async def get_users(session: AsyncSession = Depends(get_db)) -> Sequence[User]:
4243
"""Get all users."""
4344
result = await session.execute(select(User))

schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Set up some schemas for the database."""
2+
from pydantic import BaseModel
3+
4+
5+
class UserResponseModel(BaseModel):
6+
"""Response model for the User model."""
7+
8+
# id: int
9+
name: str
10+
email: str

0 commit comments

Comments
 (0)