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

paginate expecting a different type than it gets for its items #1142

Open
YarrikV opened this issue Apr 29, 2024 · 1 comment
Open

paginate expecting a different type than it gets for its items #1142

YarrikV opened this issue Apr 29, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@YarrikV
Copy link

YarrikV commented Apr 29, 2024

The project I am working on has schemas used in the api and models which are stored in the database (using SQLAlchemy as interface).

The schemas and models are different because of reasons which are irrelevant to this issue.

Current behavior

I am implementing pagination, but the issue is that paginate() is expecting its items to be of the schema-type, and it knows this from the endpoint details, while I first fetch them as models on paginate.

I can go around this by transforming the items to the schema's. However, this is not ideal since I want to do some things inbetween those two actions (fetching items with paginate and transforming the items into the schema's).

Expected behavior

I would love to be able to specify somewhere which pydantic model the paginate function will expect.

@uriyyo uriyyo self-assigned this Apr 29, 2024
@uriyyo uriyyo added the enhancement New feature or request label Apr 29, 2024
@uriyyo
Copy link
Owner

uriyyo commented Apr 29, 2024

Hi @YarrikV,

I guess you can try to use set_page function, it can be used with with statement to set page class for a specific context.

Here is an example:

@app.get('/users')
def get_users(db: Session = Depends(get_db)) -> Page[UserOut]:
    with set_page(Page[User]):
        page = paginate(db, select(User).order_by(User.created_at))

    # here you can do something with page, items will pe of User type

    return page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants