odata-query is a library that parses OData v4 filter strings, and can convert them to other forms such as Django Queries, SQLAlchemy Queries, or just plain SQL.
odata-query is available on pypi, so can be installed with the package manager
of your choice:
pip install odata-query # OR poetry add odata-query # OR pipenv install odata-query
The package defines the following optional
django: If you want to pin a compatible Django version.
sqlalchemy: If you want to pin a compatible SQLAlchemy version.
extras relate to the development of this library:
linting: The linting and code style tools.
testing: Packages for running the tests.
docs: For building the project documentation.
You can install
extras by adding them between square brackets during
pip install odata-query[sqlalchemy]
The most common use case is probably parsing an OData query string, and applying
it to a query your ORM understands. For this purpose there is an all-in-one function:
Example for Django:
from odata_query.django import apply_odata_query orm_query = MyModel.objects # This can be a Manager or a QuerySet. odata_query = "name eq 'test'" # This will usually come from a query string parameter. query = apply_odata_query(orm_query, odata_query) results = query.all()
Example for SQLAlchemy:
from odata_query.sqlalchemy import apply_odata_query orm_query = select(MyModel) # This is any form of Query or Selectable. odata_query = "name eq 'test'" # This will usually come from a query string parameter. query = apply_odata_query(orm_query, odata_query) results = session.execute(query).scalars().all()
Not all use cases are as simple as that. Luckily,
odata-query is very modular
and extensible. See the Documentation
for advanced usage or extending the library for other cases.