Lightweight library for providing filtering mechanism for your APIs using SQLAlchemy

Overview

example workflow example workflow codecov

sqlalchemy-filters-plus is a light-weight extendable library for filtering queries with sqlalchemy.

Install

pip install sqlalchemy-fitlers-plus

Usage

This library provides an easy way to filter your SQLAlchemy queries, which can for example be used by your users as a filtering mechanism for your exposed models via an API.

Let's define an example of models that will be used as a base query.

from sqlalchemy import Column, Date, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref

Base = declarative_base()


class User(Base):
    id = Column(Integer, primary_key=True)
    email = Column(String)
    age = Column(Integer)
    birth_date = Column(Date, nullable=False)


class Article(Base):
    id = Column(Integer, primary_key=True)
    title = Column(String)
    user_id = Column(Integer, ForeignKey(User.id), nullable=False)
    user = relationship(
        User,
        uselist=False,
        lazy="select",
        backref=backref("articles", uselist=True, lazy="select"),
    )

Define your first filter

Let's then define our first Filter class for the Article model

from sqlalchemy_filters import Filter, StringField
from sqlalchemy_filters.operators import ContainsOperator


class ArticleFilter(Filter):
    title = StringField(lookup_operator=ContainsOperator)
    email = StringField(field_name="user.email")

    class Meta:
        model = Article
        session = my_sqlalchemy_session

The example above defines a new filter class attached to the Article model, we declared two fields to filter with, title with the lookup_operator ContainsOperator and an email field which points to the user's email, hence the field_name="user.email" without any lookup_operator (default value is EqualsOperator) that will be used to filter with on the database level. We will see other operators that can also be used.

To apply the filter class, we instantiate it and pass it the data(as a dictionary) to filter with.

my_filter = ArticleFilter(data={"email": "[email protected]", "title": "python"})
query = my_filter.apply()  # query is a SQLAlchemy Query object

Please read the full documentation here https://sqlalchemy-filters-plus.readthedocs.io/

You might also like...
flask-apispec MIT flask-apispec (šŸ„‰24 Ā· ā­ 520) - Build and document REST APIs with Flask and apispec. MIT

flask-apispec flask-apispec is a lightweight tool for building REST APIs in Flask. flask-apispec uses webargs for request parsing, marshmallow for res

Flask + marshmallow for beautiful APIs

Flask-Marshmallow Flask + marshmallow for beautiful APIs Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmal

A template for Flask APIs.
A template for Flask APIs.

FlaskAPITempate A template for a Flask API. Why tho? I just wanted an easy way to create a Flask API. How to setup First, use the template. You can do

Seamlessly serve your static assets of your Flask app from Amazon S3

flask-s3 Seamlessly serve the static assets of your Flask app from Amazon S3. Maintainers Flask-S3 is maintained by @e-dard, @eriktaubeneck and @SunDw

Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application.

Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of

A basic JSON-RPC implementation for your Flask-powered sites
A basic JSON-RPC implementation for your Flask-powered sites

Flask JSON-RPC A basic JSON-RPC implementation for your Flask-powered sites. Some reasons you might want to use: Simple, powerful, flexible and python

:rocket: Generate a Postman collection from your Flask application
:rocket: Generate a Postman collection from your Flask application

flask2postman A tool that creates a Postman collection from a Flask application. Install $ pip install flask2postman Example Let's say that you have a

Adds GraphQL support to your Flask application.

Flask-GraphQL Adds GraphQL support to your Flask application. Usage Just use the GraphQLView view from flask_graphql from flask import Flask from flas

A basic JSON-RPC implementation for your Flask-powered sites
A basic JSON-RPC implementation for your Flask-powered sites

Flask JSON-RPC A basic JSON-RPC implementation for your Flask-powered sites. Some reasons you might want to use: Simple, powerful, flexible and python

Comments
  • ArgumentError on pre-joined query

    ArgumentError on pre-joined query

    I'm not totally sure what's causing this so apologies for the somewhat vague issue. Hopefully you'll have a better idea!

    I have a query with a couple of joins on it already and when I attempt to filter it, I'm getting the following exception:

      File "/usr/local/lib/python3.9/site-packages/sqlalchemy_filters/filters.py", line 563, in apply
        query = self._apply_join(query)
      File "/usr/local/lib/python3.9/site-packages/sqlalchemy_filters/filters.py", line 364, in _apply_join
        if is_already_joined(query, join[0]):
      File "/usr/local/lib/python3.9/site-packages/sqlalchemy_filters/utils.py", line 37, in is_already_joined
        return join_table in [_[0] for _ in query._legacy_setup_joins]
      File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/operators.py", line 366, in __eq__
        return self.operate(eq, other)
      File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/attributes.py", line 317, in operate
        return op(self.comparator, *other, **kwargs)
      File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/relationships.py", line 1302, in __eq__
        self.property._optimized_compare(
      File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/relationships.py", line 1690, in _optimized_compare
        raise sa_exc.ArgumentError(
    sqlalchemy.exc.ArgumentError: Mapped instance expected for relationship comparison to object.   Classes, queries and other SQL elements are not accepted in this context; for comparison with a subquery, use DataSource.owner.has(**criteria).
    
    opened by TWeatherston 3
  • Using filter on query with a join creates a duplicate join

    Using filter on query with a join creates a duplicate join

    I have these two related models eg:

    class Organisation(Base):
        __tablename__ = "organisations"
        id = Column(INTEGER, primary_key=True)
        name = Column(VARCHAR(64))
    
    
    class User(Base):
        __tablename__ = "users"
        id = Column(INTEGER, primary_key=True)
        name = Column(VARCHAR(64))
        email = Column(VARCHAR(64))
        organisation_id = Column(Integer, ForeignKey("organisations.id"))
        organisation = relationship("Organisation")
    

    And then a filter for filtering users:

    class UserFilter(Filter):
        name = Field()
        email = Field(lookup_operator=ContainsOperator)
        organisation = Field(field_name="organisation.name")
    
        class Meta:
            model = User
    

    If I then create a query with a join and attempt to apply the filter to the query:

    query = db_session.query(User).join(Organisation)
    my_filter = UserFilter(data={"organisation": "test_org"}, query=query)
    query = my_filter.apply()
    print(str(query))
    

    It creates this second joining of the organisations table:

    SELECT 
        users.id AS users_id, 
        users.name AS users_name, 
        users.email AS users_email, 
        users.organisation_id AS users_organisation_id
    FROM users 
        JOIN organisations ON organisations.id = users.organisation_id
        JOIN organisations ON organisations.id = users.organisation_id
    WHERE organisations.name = ?
    
    opened by TWeatherston 1
  • Ordering by UnaryExpression raises TypeError

    Ordering by UnaryExpression raises TypeError

    Hey,

    Really love the package, great work! :+1:

    I've just been having some difficulty using the order_by with a UnaryExpression. Example from the docs:

    MyFilter(data={"order_by": User.first_name.asc()})
    

    This results in:

    TypeError: Boolean value of this clause is not defined

    It seems that this is the offending line. Would it be possible to get this changed to something like this?

    order_by = self.data.get("order_by", self._order_by)
    

    Here's a minimal working example that I was using to test this:

    from sqlalchemy import Column, VARCHAR, INTEGER, create_engine
    from sqlalchemy.orm import declarative_base, Session
    from sqlalchemy_filters import Filter, Field
    
    
    Base = declarative_base()
    
    
    class User(Base):
        __tablename__ = "users"
        id = Column(INTEGER, primary_key=True)
        name = Column(VARCHAR(64))
        email = Column(VARCHAR(64))
    
    
    class UserFilter(Filter):
        name = Field()
    
        class Meta:
            model = User
    
    
    engine = create_engine("sqlite://")
    with Session(engine) as session:
        query = session.query(User)
        my_filter = UserFilter(data={"order_by": User.name.desc()}, query=query)
        query = my_filter.apply()
    

    Thanks!

    opened by TWeatherston 1
  • Doesn't seem to apply the specific filter

    Doesn't seem to apply the specific filter

    Hi, Using your library, and this seemed to be not filtering with address_id and business_id base_filter = ScreeningHistoryFilter(data={"address_id":f"{address_id}", "business_id":f"{business_id}", "page":pagination_params.page_index, "page_size": pagination_params.page_size, "order_by": sort_params.sort_fields}, session= self.db)

    Below is the filter class: `class ScreeningHistoryFilter(Filter): address_id: Field() business_id: Field() # start_range: DateTimeField(field_name ='create_tz', lookup_operator = GTEOperator) # end_range: DateTimeField(field_name ='create_tz', lookup_operator = LTEOperator)

    class Meta:
        model = V_Address_Screening_History`
    
    opened by jaballe 4
Releases(1.1.5)
Owner
Karami El Mehdi
Karami El Mehdi
Control YouTube, streaming sites, media players on your computer using your phone as a remote.

Media Control Control Youtube, streaming sites, media players on your computer using your phone as a remote. Installation pip install -r requirements.

Shreyas Daniel 10 Dec 08, 2022
This is a Flask web app which predicts fare of Flight ticket

Flight Fare Prediction: Table of Content Demo Overview Motivation Installation Deployement on Heroku Directory Tree Bug / Feature Request Future scope

Ayshwarya 1 Jan 24, 2022
A team blog based on Flask

A team blog based on Flask This project isn't supported at the moment, please see a newer pypress-tornado Thanks for flask_website and newsmeme at [ht

老ē§‹ 549 Nov 10, 2022
Flask extension that takes care of API representation and authentication.

Flask-API-Utils Flask-API-Utils helps you to create APIs. It makes responses in appropriate formats, for instance, JSON. All you need to do is to retu

Marsel Mavletkulov 55 Aug 28, 2022
:rocket: Generate a Postman collection from your Flask application

flask2postman A tool that creates a Postman collection from a Flask application. Install $ pip install flask2postman Example Let's say that you have a

Numberly 137 Nov 08, 2022
Formatting of dates and times in Flask templates using moment.js.

Flask-Moment This extension enhances Jinja2 templates with formatting of dates and times using moment.js. Quick Start Step 1: Initialize the extension

Miguel Grinberg 358 Nov 28, 2022
Making a simple app using React, Flask and MySQL.

Samys-Cookbook Making a simple app using React and Flask. What This will be a simple site to host my recipes. It will have a react front-end, a flask

Samridh Anand Paatni 1 Jul 07, 2022
Flask-app scaffold, generate flask restful backend

Flask-app scaffold, generate flask restful backend

jacksmile 1 Nov 24, 2021
Full-Stack application that visualizes amusement park safety.

Amusement Park Ride Safety Analysis Project Proposal We have chosen to look into amusement park data to explore ride safety relationships visually, in

Michael Absher 0 Jul 11, 2021
YAML-formatted plain-text file based models for Flask backed by Flask-SQLAlchemy

Flask-FileAlchemy Flask-FileAlchemy is a Flask extension that lets you use Markdown or YAML formatted plain-text files as the main data store for your

Siddhant Goel 20 Dec 14, 2022
Criando um Bot com PYAUTOGUI e utilizando o Flask para Interface para UsuƔrio

Criando um Bot com PYAUTOGUI e utilizando o Flask para Interface para UsuĆ”rio O pyautogui foi escolhido pela possibilidade de fazer a identificaĆ§Ć£o do

Rodrigo Vital 2 Oct 20, 2021
Flask starter template for better structuring.

Flask Starter app Flask starter template for better structuring. use the starter plate step 1 : cloning this repo through git clone the repo git clone

Tirtharaj Sinha 1 Jul 26, 2022
Adds Injector support to Flask.

Flask-Injector Adds Injector support to Flask, this way there's no need to use global Flask objects, which makes testing simpler. Injector is a depend

Alec Thomas 246 Dec 28, 2022
Flask webassets integration.

Integrates the webassets library with Flask, adding support for merging, minifying and compiling CSS and Javascript files. Documentation: https://flas

Michael Elsdƶrfer 433 Dec 29, 2022
Flask-Diamond is a batteries-included Flask framework.

Flask-Diamond Flask-Diamond is a batteries-included Python Flask framework, sortof like Django but radically decomposable. Flask-Diamond offers some o

Diamond Methods 173 Dec 22, 2022
This is a simple web application using Python Flask and MySQL database.

Simple Web Application This is a simple web application using Python Flask and MySQL database. This is used in the demonstration of development of Ans

Alaaddin Tarhan 1 Nov 16, 2021
A Flask web application that manages student entries in a SQL database

Student Database App This is a Flask web application that manages student entries in a SQL database. Users can upload a CSV into the SQL database, mak

rebecca 1 Oct 20, 2021
Free casino website. Madden just for learning / fun

Website Casino Free casino website. Madden just for learning / fun. Uses Jinja2 (HTML), Flask, JavaScript, etc. Dice game Preview

Kirill Zhosul 0 Jun 22, 2022
A template for Flask APIs.

FlaskAPITempate A template for a Flask API. Why tho? I just wanted an easy way to create a Flask API. How to setup First, use the template. You can do

TechStudent10 1 Dec 28, 2021
Small flask based opds catalog designed to serve a directory via OPDS

teenyopds Small flask based opds catalog designed to serve a directory via OPDS, it has currently only been verified to work with KyBook 3 on iOS but

Adam Furbee 4 Jul 14, 2022