Router Prefix
Complex APIs can have very complex and long routes
Router Prefixes allow us to have that stored
We can additionally replace the route with
"/"
# Stored Prefix
router = APIRouter(
prefix="/posts"
)
# We no longer need "/posts" and can replace it with "/"
# Before:
@router.get("/posts", response_model=List[schemas.PostResponse])
def get_posts(db: Session = Depends(get_db)):
posts = db.query(models.Post).all()
return posts
# After:
@router.get("/", response_model=List[schemas.PostResponse])
def get_posts(db: Session = Depends(get_db)):
posts = db.query(models.Post).all()
return postsHow it looks with the full code for the users.py file
Last updated