sqlalchemy-filters-plus


Namesqlalchemy-filters-plus JSON
Version 1.1.5 PyPI version JSON
download
home_pagehttps://github.com/elmkarami/sqlalchemy-filters-plus
SummarySQLAlchemy filters made easy
upload_time2022-12-03 15:45:54
maintainer
docs_urlNone
authorEl Mehdi Karami
requires_python>=3.6
licenseBSD
keywords sqlalchemy filter flask python sql query
VCS
bugtrack_url
requirements coverage pytest pytest-cov factory-boy pytest-cov faker marshmallow tox black
Travis-CI
coveralls test coverage No coveralls.
            ![example workflow](https://github.com/elmkarami/sqlalchemy-filters-plus/actions/workflows/release.yml/badge.svg)
![example workflow](https://github.com/elmkarami/sqlalchemy-filters-plus/actions/workflows/main.yml/badge.svg)
[![codecov](https://codecov.io/gh/elmkarami/sqlalchemy-filters-plus/branch/master/graph/badge.svg?token=I7ZC1WQYEQ)](https://codecov.io/gh/elmkarami/sqlalchemy-filters-plus)

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

Install
-

```bash
pip install sqlalchemy-filters-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.

```python
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

```python
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.

```python
my_filter = ArticleFilter(data={"email": "some@email.com", "title": "python"})
query = my_filter.apply()  # query is a SQLAlchemy Query object
```
    




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



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/elmkarami/sqlalchemy-filters-plus",
    "name": "sqlalchemy-filters-plus",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "sqlalchemy,filter,flask,python,sql,query",
    "author": "El Mehdi Karami",
    "author_email": "me@elmkarami.com",
    "download_url": "https://files.pythonhosted.org/packages/dd/e6/7791fca532169c97f41801659fe02c04346b33cf9cfe8a9a7aece5f1ec20/sqlalchemy-filters-plus-1.1.5.tar.gz",
    "platform": null,
    "description": "![example workflow](https://github.com/elmkarami/sqlalchemy-filters-plus/actions/workflows/release.yml/badge.svg)\n![example workflow](https://github.com/elmkarami/sqlalchemy-filters-plus/actions/workflows/main.yml/badge.svg)\n[![codecov](https://codecov.io/gh/elmkarami/sqlalchemy-filters-plus/branch/master/graph/badge.svg?token=I7ZC1WQYEQ)](https://codecov.io/gh/elmkarami/sqlalchemy-filters-plus)\n\nsqlalchemy-filters-plus is a light-weight extendable library for filtering queries with sqlalchemy.\n\nInstall\n-\n\n```bash\npip install sqlalchemy-filters-plus\n```\n\n\nUsage\n-----\n\nThis library provides an easy way to filter your SQLAlchemy queries,\nwhich can for example be used by your users as a filtering mechanism for your exposed models via an API.\n\nLet's define an example of models that will be used as a base query.\n\n```python\nfrom sqlalchemy import Column, Date, Integer, String, ForeignKey\nfrom sqlalchemy.ext.declarative import declarative_base\nfrom sqlalchemy.orm import relationship, backref\n\nBase = declarative_base()\n\n\nclass User(Base):\n    id = Column(Integer, primary_key=True)\n    email = Column(String)\n    age = Column(Integer)\n    birth_date = Column(Date, nullable=False)\n\n\nclass Article(Base):\n    id = Column(Integer, primary_key=True)\n    title = Column(String)\n    user_id = Column(Integer, ForeignKey(User.id), nullable=False)\n    user = relationship(\n        User,\n        uselist=False,\n        lazy=\"select\",\n        backref=backref(\"articles\", uselist=True, lazy=\"select\"),\n    )\n```\n\n\nDefine your first filter\n========================\n\nLet's then define our first Filter class for the Article model\n\n```python\nfrom sqlalchemy_filters import Filter, StringField\nfrom sqlalchemy_filters.operators import ContainsOperator\n\n\nclass ArticleFilter(Filter):\n    title = StringField(lookup_operator=ContainsOperator)\n    email = StringField(field_name=\"user.email\")\n\n    class Meta:\n        model = Article\n        session = my_sqlalchemy_session\n```\n\n\nThe example above defines a new filter class attached to the Article model, we declared two fields to filter with, \n``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.\n\nTo apply the filter class, we instantiate it and pass it the data(as a dictionary) to filter with.\n\n```python\nmy_filter = ArticleFilter(data={\"email\": \"some@email.com\", \"title\": \"python\"})\nquery = my_filter.apply()  # query is a SQLAlchemy Query object\n```\n    \n\n\n\n\nPlease read the full documentation here https://sqlalchemy-filters-plus.readthedocs.io/\n\n\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "SQLAlchemy filters made easy",
    "version": "1.1.5",
    "split_keywords": [
        "sqlalchemy",
        "filter",
        "flask",
        "python",
        "sql",
        "query"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "72bb83257c368effcda4931436996ee1",
                "sha256": "a06cbf3b5fc288ec2eaa75f088eca3d525d2841e0070e80a7abaf686e615f0b3"
            },
            "downloads": -1,
            "filename": "sqlalchemy-filters-plus-1.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "72bb83257c368effcda4931436996ee1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 16321,
            "upload_time": "2022-12-03T15:45:54",
            "upload_time_iso_8601": "2022-12-03T15:45:54.082384Z",
            "url": "https://files.pythonhosted.org/packages/dd/e6/7791fca532169c97f41801659fe02c04346b33cf9cfe8a9a7aece5f1ec20/sqlalchemy-filters-plus-1.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-03 15:45:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "elmkarami",
    "github_project": "sqlalchemy-filters-plus",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "coverage",
            "specs": [
                [
                    "==",
                    "5.3"
                ]
            ]
        },
        {
            "name": "pytest",
            "specs": [
                [
                    "==",
                    "6.2.5"
                ]
            ]
        },
        {
            "name": "pytest-cov",
            "specs": [
                [
                    "==",
                    "2.10.1"
                ]
            ]
        },
        {
            "name": "factory-boy",
            "specs": [
                [
                    "==",
                    "3.2.0"
                ]
            ]
        },
        {
            "name": "pytest-cov",
            "specs": [
                [
                    "==",
                    "2.10.1"
                ]
            ]
        },
        {
            "name": "faker",
            "specs": [
                [
                    "==",
                    "8.14.0"
                ]
            ]
        },
        {
            "name": "marshmallow",
            "specs": [
                [
                    "==",
                    "3.10.0"
                ]
            ]
        },
        {
            "name": "tox",
            "specs": [
                [
                    "==",
                    "3.23.0"
                ]
            ]
        },
        {
            "name": "black",
            "specs": [
                [
                    "==",
                    "20.8b1"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "sqlalchemy-filters-plus"
}
        
Elapsed time: 0.25410s