Search code examples
eve

Add a filter with OR using sqlalchemy in eve pre_GET hook


I am using Eve with the sqlalchemy branch to develop a REST API.

I want to add a filter to a GET request before the request is sent to the database, however I have found no way to do this yet.

What I want to do is basically this:

from sqlalchemy import or_

def pre_GET(resource, request, lookup):
    lookup.append(_or('field1'==1, 'field2'==2))

Of course this does not work, as lookup is a dictionary. However searching to the eve source code I can see no possibility to realize an OR. The lookup variable is parsed with parse_dictionary() in this file: https://github.com/nicolaiarocci/eve/blob/sqlalchemy/eve/io/sql/parser.py

The corresponding way when using MongoDB would be this:

lookup['$or'] = [{'field1': 1}, {'field2': 2}]

However this only works with MongoDB...


Solution

  • I understand this is an old question. eve-sqlalchemy is now an extension with separate lifecycle/code base.

    Current code base seems to handle or condition here

    May be it works now ?


    Update (Feb 06, 2016)

    I tested this. Following syntax works :

    lookup['or_'] = [{'field1': 1}, {'field2': 2}]
    

    Notice or_ instead of $or you expected.