AME
AME

Reputation: 2609

How to use query.filter and .filter_by in SQLAlchemy?

With Django it is possible to find models using the filter method with keyword-arguments like so:

MyModel.objects.filter(serialNo_gt=10)

giving all models with a serial number greater than 10.

Is it possibly to use a similar query language with sql-alchemy? I know that one can write something like MyModel.seriealNO < 10, but with that the code that uses this construct need to import MyModel and I want to create the keywords/query parameters externally without importing MyModel (for a facade-pattern).

Upvotes: 1

Views: 1453

Answers (1)

zzzeek
zzzeek

Reputation: 75107

the concept of "<attributename>_<operatorname>=<value>" is not built in to SQLAlchemy's Query, however the effect is very easy to reproduce. Here's a quick example done by the author of Flask: https://github.com/mitsuhiko/sqlalchemy-django-query/blob/master/sqlalchemy_django_query.py

Upvotes: 3

Related Questions