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.9

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.9
File Size Uploaded
flask_smorest_sqlalchemy_odata-0.2.9.tar.gz 10.2 kB Details

Built distribution (wheel)

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

Total release size: 18.9 kB

Release files / flask_smorest_sqlalchemy_odata-0.2.9.tar.gz

Download URL flask_smorest_sqlalchemy_odata-0.2.9.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
60e1ff13b880e7b1d0c794d0160e0b1dcc680a97d2381d02682ae987be78541e
BLAKE2b-256 checksum
How to use checksums
b423d2f7b3b25ffa00e9858d3afdc73d519990b531005559209ea1ffe5a098d4
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.9-py3-none-any.whl

Download URL flask_smorest_sqlalchemy_odata-0.2.9-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe003ac66e7241034b43950c5ece1a357c3ce981c9beb3940224e3281987cc40
BLAKE2b-256 checksum
How to use checksums
d1b428724e62cdbf67d75625bb5fefc1f99a0bc58aa41e210c4b012b50076422
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

0.2.10

2 release files

This release

0.2.9 This release

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