Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request: Get List of items. #4

Closed
paulr2021 opened this issue Dec 24, 2021 · 1 comment
Closed

Request: Get List of items. #4

paulr2021 opened this issue Dec 24, 2021 · 1 comment

Comments

@paulr2021
Copy link

paulr2021 commented Dec 24, 2021

With this as an example, how would you return a list of items?

`@router.get("/", response_model=List[schemas.Cat])
async def read_cat(
cat: schemas.Cat,
session: AsyncSession = Depends(deps.get_session),
current_user: models.User = Depends(deps.get_current_user),
) -> List:
"""
Get cats.
"""

return cat`
@paulr2021 paulr2021 changed the title Request: Get List if items. Request: Get List of items. Dec 24, 2021
@rafsaf
Copy link
Owner

rafsaf commented Dec 27, 2021

Your question is not precise at all, I strongly suggest using precise words and proper code formatting when creating issues on GitHub ;) At least I can guess.

Standard way is to use SQLAlchemy (1.4 style) query:

@router.get("/", response_model=List[schemas.Cat])
async def read_cats(
    offset: int = 0,
    limit: int = 100,
    current_user: models.User = Depends(deps.get_current_user),
    session: AsyncSession = Depends(deps.get_session),
):
    """
    Get all cats
    """
    cats_result = await session.execute(select(models.Cat).offset(offset).limit(limit))
    return cats_result.scalars().all()

@rafsaf rafsaf closed this as completed Dec 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants