SQLAlchemy filter converters.
Project description
SQLAlchemy filter converter
For what?
I made this project to avoid copy-pasting with utils in my projects. I was aiming to simplify working with sqlalchemy, FastAPI and other libraries.
Install
To install the package you need you run the following commands.
For pip:
pip install "sqlalchemy_filter_converter"
For poetry:
poetry add sqlalchemy_filter_converter
For PDM:
pdm add sqlalchemy_filter_converter
Filter Types
Converters for SQLAlchemy filters. Now available 3 converters:
- Simple filter converter (key-value with equals operator).
- Advanced filter converter (key-value with custom operators).
- Django filter converter (Django filters adapter with double underscore lookups).
Filters must be provided in a dict or list or dicts and will be applied sequentially.
Simple filters
Simple filters are simle. There are key
- value
dicts, which converts to SQLAlchemy
filters with ==
operator.
You can use one dict with all filters, or list of dicts. There is no difference.
from sqlalchemy_filter_converter import SimpleFilterConverter
filter_spec = [
{'field_name_1': 123, 'field_name_2': 'value'},
{'other_name_1': 'other_value', 'other_name_2': 123},
# ...
]
No other specific usages presents. it is simple.
Advanced filters
Advanced filters continues the idea of simple-filter, but add operator key.
{
"field": "my_field",
"value": 25,
"operator": ">",
}
or
[
{
"field": "my_id_field",
"value": [1,2,3,4,5],
"operator": "contains",
},
{
"field": "my_bool_field",
"value": False,
"operator": "is_not",
},
]
This is the list of operators that can be used:
=
>
<
>
'<=
is
is_not
between
contains
Django filters
Django filters implements django ORM adapter for filters. You can use filters like
my_field__iexact=25
or my_dt_field__date__ge=datetime.date(2023, 3, 12)
. See django
documentation for more information.
Now implements all field filters, except nester relationships.
This is the list of operators that can be used:
exact
iexact
contains
icontains
in
gt
gte
lt
lte
startswith
istartswith
endswith
iendswith
range
date
year
iso_year
month
day
week
week_day
iso_week_day
quarter
time
hour
minute
second
isnull
regex
iregex
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sqlalchemy_filter_converter-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7704d7212d50bffe1293402771a6f3509333e2c0a0b73bea26a9f9688107c0db |
|
MD5 | a4e5b4676e46cc93655c03781885fe7e |
|
BLAKE2b-256 | f3cf71fc0c0382fa2084636298930ceaa98e354a1b7f9311c4a5b495829d3144 |
Hashes for sqlalchemy_filter_converter-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2168ffe748aaf161a43bae66fa8e04c55577357036f9d3acfdf3569ad3a1d17 |
|
MD5 | 6d8357678b0c6365fb201b708543d3bf |
|
BLAKE2b-256 | 27b093a82b07b8154e46e9b75fe5965e6cf3d58c41f4ffd83c34fa588f71f5f5 |