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

How to implement a generic Filter #1199

Open
eugenenelou opened this issue Jun 20, 2024 · 1 comment
Open

How to implement a generic Filter #1199

eugenenelou opened this issue Jun 20, 2024 · 1 comment

Comments

@eugenenelou
Copy link

I want to implement a generic filter that will allow me to filter by multiple possible values.

for example query fruits of a certain type with: /fruits?type=apple,banana,plum

this is the implementation that almost works, the only problem is that I would have to pass q="type__in" and this would not be generic.
I think that a q_suffix (I don't care about the name) would permit to make this generic by making the name of the field implicit.

from typing import Annotated, TypeVar

from pydantic import BaseModel, Field, BeforeValidator


T = TypeVar("T")

InFilter = Annotated[
    list[T], BeforeValidator(lambda v: v.split(",")), Field(q_suffix="__in")
]

class Filters(FilterSchema):
  type: InFilter[str]

So did I miss something and is that already possible?

@eugenenelou
Copy link
Author

eugenenelou commented Jun 28, 2024

I opened a PR for this #1212

instead of adding q_suffix, I just allowed the field name to be implicit, which is a lot lighter.

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

1 participant