miLibris / flask-rest-jsonapi

Flask extension to build REST APIs around JSONAPI 1.0 specification.
http://flask-rest-jsonapi.readthedocs.io
MIT License
597 stars 153 forks source link

How to filter by null relationship? #38

Closed michaelgodshall closed 7 years ago

michaelgodshall commented 7 years ago

How can I filter a relationship using a null value? Flask-Restless usesis_null or is_not_null, but there doesn't seem to be a similar operator in Flask-Rest-JSONAPI. Any recommendations on how to achieve the same result in Flask-Rest-JSONAPI?

michaelgodshall commented 7 years ago

I'm trying to use the is_ operator, but passing different values like 'None' or false doesn't work.

akira-dev commented 7 years ago

I released a new version 0.12.5 so now you can try this:

First you can try this:

?filter=[{"name":"relation","op":"eq","val":null}]

it means: filter(Model.relation == None)

If it doesn't work you can try this:

?filter=[{
    "not": {
        "name":"relation",
        "op":"any",
        "val": null
    }
}]

this means with sqlalchemy : filter(~Model.relation.any(None))

and in SQL:

SELECT * FROM Model
         WHERE NOT (EXISTS (SELECT 1
                            FROM RelatedModel
                            WHERE Model.foreign_key = RelatedModel.foreign_key))
AndreNalevaiko commented 6 years ago

Has the fix occurred only on this operator? I have the same problem in the filter: {"name":"category_id","op":"in","val":[1,null]}