Usage

The entirety of sqlalchemy-querybuilder’s functionality is encapsulated in the Filter class.

Getting started

Filter class accepts three parameters:

  • models (required) can be either a module defining classes which inherit from declarative_base or a dictionary of such classes with the name of the tables as keys.
  • query (required) is a SQLAlchemy query object. Optionaly loaded with some entity.
  • operators (optional) is a dictionary of operators. By default all operators are enabled. This is how they are all implemented:
    • equal__eq__(x)
    • not_equal__ne__(x)
    • less__lt__(x)
    • greater__gt__(x)
    • less_or_equal__le__(x)
    • greater_or_equal__ge__(x)
    • inin_(x)
    • not_innotin_(x)
    • ends_withlike('%' + x)
    • begins_withlike(x + '%')
    • containslike('%' + x + '%')
    • not_containsnotlike('%' + x + '%')
    • not_begins_withnotlike(x + '%')
    • not_ends_withnotlike('%' + x)
    • is_empty__eq__('')
    • is_not_empty__ne__('')
    • is_nullis_(None)
    • is_not_nullisnot(None)
    • betweenbetween(x[0], x[1])

Filter only has one method: querybuilder. This method only accepts rules parameter, which is a dictionary that holds rules processed by jquery QueryBuilder or compatible.

Examples

Given the file models.py containing the following mappings:

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class MyModel(Base):
    __tablename__ = 'test1'
    parentid = Column('test1id', Integer, primary_key=True)

You could use sqlalchemy-querybuild like:

Using a module

from sqlalchemy_querybuilder import Filter
import models

rule = {
        "condition": "OR",
        "rules": [{
                    "id": "price",
                    "field": "test1.parentid",
                    "type": "integer",
                    "input": "number",
                    "operator": "equal",
                    "value": "1"
                   },
                  ],
}
filter = Filter(models, session.query())
print(filter.querybuilder(rule)

Using a dictionary instead of a module

from sqlalchemy.orm import Session
from sqlalchemy_querybuilder import Filter
import models

rule = {
        "condition": "OR",
        "rules": [{
                    "id": "price",
                    "field": "test1.parentid",
                    "type": "integer",
                    "input": "number",
                    "operator": "equal",
                    "value": "1"
                   },
                  ],
}
filter = Filter({"test1": models.MyModel}, session.query())
print(filter.querybuilder(rule)

Using a previously loaded query

from sqlalchemy_querybuilder import Filter
import models
import other

rule = {
        "condition": "OR",
        "rules": [{
                    "id": "price",
                    "field": "test1.parentid",
                    "type": "integer",
                    "input": "number",
                    "operator": "equal",
                    "value": "1"
                   },
                  ],
}
filter = Filter(models, session.query(other.MyOtherModel))
print(filter.querybuilder(rule)