Skip to main content

Odata filtering for Flask-Smorest with Flask-Sqlalchemy

Add odata-like filtering and sorting on top of flask-smorest functionality

Usage

The primary use case is as a wrapper around the flask-smorest Blueprint class. It will add the odata filter and orderby query params and apply them to the returned model from a MethodView.

from http import HTTPStatus
from flask_smorest import Api, Page
from flask.views import MethodView

from odata import Blueprint

api = Api(app)


class CursorPage(Page):
    @property
    def item_count(self):
        return self.collection.count()


resources = Blueprint(
    'resources',
    __name__,
    url_prefix='/',
    description='root resources',
)

@resources.route('/users')
class User(MethodView):

    @resources.response(HTTPStatus.OK, schemas.User(many=True))
    @resources.paginate(CursorPage)
    @resources.odata(db.session)
    def get(self):
        return models.User

api.register_blueprint(resources)

It will also add docs so the params will appear in Swagger/Redoc.

Features

Filter operators

Multiple filter operations can be joined with and and or

Operator Name Syntax Examples
Contains contains(field,'value') contains(description,'middle of a sentence')
Equal field eq value id eq 1
isActive eq true
isActive eq false
serialNumber eq null
description eq 'very specific'
Not Equal field ne value isActive ne true
In field in (comma,separated,values) id in (1,3)
username in ("user1", "user2")
Starts with startswith(field,'value') startswith(preamble,'We the people')
Ends with endswith(field,'value') endswith(preamble,'United States of America.')
Greater than field gt value fingers gt 5
created gt 2020-01-05T00:00:00
Less than field lt value fingers lt 5
Greater than or equal to field ge value fingers ge 5
Less than or equal to field le value fingers le 5

Filter by joined properties

It is possible to filter by a joined property, even if that property isn't returned in the payload. Use a forward-slash (/) to indicate a join.

For example: /users?filter=roles/name eq "admin" would return all users that have a related role with name 'admin'.

Works for one-to-many and many-to-many.

To search for empty or non-empty relations, use eq null or ne null respectively. For example, to filter for users with no roles assigned: /users?filter=roles eq null

Ordering

Use the orderby query parameter to sort by a top-level property or joined property, ascending or descending.

Examples:

  • /users?orderby=id
  • /users?orderby=id desc
  • /users?orderby=supervisor/username
  • /users?filter=isActive eq true&orderby=logins desc

AND / OR

Filters can be combined with and and or. Nested filtering with parens is allowed.

Examples:

  • isActive eq true or isActive eq false
  • createdTime ge 2021-01-01T00:00:00 and createdTime le 2021-02-01T23:59:59 and userId eq 1
  • (username eq 'user2' and logins eq 100 and isActive eq false) or (logins gt 1 and username eq 'user3') or contains(note,'backup') or (logins gt 1000 and username eq 'user4' and supervisor/id eq 1)

Metadata

Release files for flask-smorest-sqlalchemy-odata 0.2.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-smorest-sqlalchemy-odata 0.2.10
File Size Uploaded
flask_smorest_sqlalchemy_odata-0.2.10.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-smorest-sqlalchemy-odata 0.2.10
File Interpreter ABI Platform
flask_smorest_sqlalchemy_odata-0.2.10-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / flask_smorest_sqlalchemy_odata-0.2.10.tar.gz

Download URL flask_smorest_sqlalchemy_odata-0.2.10.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
15c302a4a0b141d7555d7f8dad817fa7dbca4c403de7f7ab76402dba8f3451a3
BLAKE2b-256 checksum
How to use checksums
90afbafcec7bd24a64bbeefa92adc5a2d105b75d40c7474bef38423df5f0938a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / flask_smorest_sqlalchemy_odata-0.2.10-py3-none-any.whl

Download URL flask_smorest_sqlalchemy_odata-0.2.10-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12cd88fb50b2525db9c4d55008492c947e38705718334e1a92e2b0c8e2fdc2ba
BLAKE2b-256 checksum
How to use checksums
7d5e9dc271eaad66cdd3f789b6341b180c05d4d2d53a74865baf6d4471644d24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release history Release notifications | RSS feed

This release

0.2.10 This release

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

1 release file

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page