AdCombo / flask-combo-jsonapi

Flask REST JSON:API on steroids.
MIT License
32 stars 16 forks source link

Filter results by search within an array attribute? #60

Closed vi0815 closed 3 years ago

vi0815 commented 3 years ago

Hello all,

I am trying to filter results by searching for a word within an array. For instance this simple model:


class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    words = db.Column(db.ARRAY(db.TEXT))

class PersonSchema(Schema):
    class Meta:
        type_ = 'person'
        self_view = 'person_detail'
        self_view_kwargs = {'id': '<id>'}
        self_view_many = 'person_list'

    id = fields.Integer(as_string=True, dump_only=True)
    words = fields.List(fields.String())

# skipping some DB code here

session.add(Person(id=1, words=["foo", "nobar", "arbitraryWord"]))
session.add(Person(id=2, words=["bar", "nofoo"]))

In this example I try to find all persons with the word "arbitraryWord" in their list of words. I hoped using the operator "in_" would work like this:

       {"name":"arbitraryWord",
        "op":"in_",
        "field":"words"}]

The result is a "Bad filter value". I tried several variations with no luck. Hope someone can give me a hint. Find full example code here: https://github.com/vi0815/exampleArraySearch

mahenzon commented 3 years ago

Hi thanks for your question, I'll provide an example later today. A agree that it's not that obvious

mahenzon commented 3 years ago

Here's a detailed example, it's not that easy because you need to define filtering by yourself, but you can create any kind of filter

https://flask-combo-jsonapi.readthedocs.io/en/develop/api_filtering_example.html