fastapi-filter icon indicating copy to clipboard operation
fastapi-filter copied to clipboard

How to filter an array field

Open raphodn opened this issue 1 year ago • 2 comments

Hi !

I'm struggling to understand if/how I can filter an array field.

I'm using SQLAlchemy + PostgreSQL, and I have a Product.categories_tags field that looks like this : ["en:beverages", "en:orange-juices].

I would like to be able to filter this field in a simple manner, for example : categories_tags__contains=en:beverages Is this something feasible ? a possible linked PR : #395

Thanks in advance for your help, I'd be willing to do a PR if needed :)

raphodn avatar Feb 21 '24 17:02 raphodn

Also interested

verhovensky avatar Apr 01 '24 21:04 verhovensky

Fyi, I ended up managing the filtering myself

  • adding categories_tags__contains: Optional[str] | None = None in my filter class
  • and having a custom filter in the crud
if filters.categories_tags__contains:
    query = query.filter(Product.categories_tags.contains([filters.categories_tags__contains]))
    filters.categories_tags__contains = None

see this PR changes : https://github.com/openfoodfacts/open-prices/pull/234/files

raphodn avatar Apr 08 '24 10:04 raphodn

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Sep 27 '24 02:09 github-actions[bot]