Skip to main content

Py-QueryBuilder

Query builder for Python applications, designed to work with the same data structure generated by MUI-QueryBuilder.

downloads versions

Installation

Py-QueryBuilder is available as a pypi package.

pip install py-querybuilder

Usage

Here is a quick example to get you started. First you'll need a templates folder in your module, containing Jinja2 templates with a {{ where }} placeholder:

SELECT *
FROM my_table
WHERE {{ where }}

You can now rely on the QueryBuilder class to render queries:

from py_querybuilder import QueryBuilder

qb = QueryBuilder("app.articles", [
    {
        "label": "Article",
        "options": [
            {
                "label": "Title",
                "value": "title",
                "type": "text",
            },
            {
                "label": "URL",
                "value": "url",
                "type": "text",
            },
        ],
    },
])
sql_query, sql_params = qb.render("query.sql", {
    "combinator": "and",
    "rules": [
        {
            "field": "title",
            "operator": "contains",
            "value": "Brazil",
        },
    ],
})

The query is generated with JinjaSQL, a template language for SQL statements and scripts. Since it's based in Jinja2, you have all the power it offers: conditional statements, macros, looping constructs, blocks, inheritance, and more. We use sqlparse to format the queries.

SELECT *
FROM my_table
WHERE title ~* ? AS "Title"

The default operators used for generating queries are:

{
    "after": ">",
    "after_equal": ">=",
    "before": "<",
    "before_equal": "<=",
    "contains": "~*",
    "greater": ">",
    "greater_equal": ">=",
    "equal": "=",
    "in": "in",
    "less": "<",
    "less_equal": "<=",
    "not_contains": "!~*",
    "not_equal": "!=",
    "not_in": "not in",
    "not_null": "is not null",
    "null": "is null",
}

In case the database you're targeting uses different operators, it's possible to customize those at QueryBuilder's instantiation:

qb = QueryBuilder(my_module, my_query, operators={
    # Custom operators.
})

SQL parameters are returned from render() in a list of values corresponding to the placeholders that need to be bound to the query.

print(sql_params)
# ["Brazil"]

Finally, you can now use the SQL query and its bind parameters to fetch data. For example, in Django, you would do something like this:

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute(sql_query, sql_params)

    for row in cursor.fetchall():
        # Do something with the results.
        pass

API

Check out the Py-QueryBuilder's API here! To learn more about the query's data structure, and how filters and operators work, please refer to MUI-QueryBuilder's documentation.

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for py-querybuilder 1.0.0

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

Built distribution (wheel)

Table of built distributions (wheels) for py-querybuilder 1.0.0
File Interpreter ABI Platform
py_querybuilder-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / py_querybuilder-1.0.0-py2.py3-none-any.whl

Download URL py_querybuilder-1.0.0-py2.py3-none-any.whl
Size 6.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7de2daaa5a2cc138a316fd80c6d0eaa66a0ee88551d76022a1921b282c654cf3
BLAKE2b-256 checksum
How to use checksums
1f8d59d4f94ce9a4c3601f7a7a9867caf9deb9993d5780940c9ab044931aec2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

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