fastapi-query-filters 0.0.1a0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_query_filters-0.0.1a0/.env.example +6 -0
- fastapi_query_filters-0.0.1a0/.gitignore +218 -0
- fastapi_query_filters-0.0.1a0/.pre-commit-config.yaml +23 -0
- fastapi_query_filters-0.0.1a0/LICENSE +21 -0
- fastapi_query_filters-0.0.1a0/PKG-INFO +297 -0
- fastapi_query_filters-0.0.1a0/README.md +276 -0
- fastapi_query_filters-0.0.1a0/compose.yaml +43 -0
- fastapi_query_filters-0.0.1a0/examples/__init__.py +0 -0
- fastapi_query_filters-0.0.1a0/examples/basic_app/__init__.py +0 -0
- fastapi_query_filters-0.0.1a0/examples/basic_app/database.py +26 -0
- fastapi_query_filters-0.0.1a0/examples/basic_app/main.py +48 -0
- fastapi_query_filters-0.0.1a0/examples/basic_app/models.py +38 -0
- fastapi_query_filters-0.0.1a0/examples/basic_app/schemas.py +54 -0
- fastapi_query_filters-0.0.1a0/examples/basic_app/seed.py +92 -0
- fastapi_query_filters-0.0.1a0/pyproject.toml +60 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/__init__.py +11 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/core.py +356 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/dependencies.py +48 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/operators.py +44 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/orm/__init__.py +5 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/orm/base.py +21 -0
- fastapi_query_filters-0.0.1a0/src/fastapi_query_filters/orm/sqlalchemy.py +218 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
# .vscode/
|
|
203
|
+
# Temporary file for partial code execution
|
|
204
|
+
tempCodeRunnerFile.py
|
|
205
|
+
|
|
206
|
+
# Ruff stuff:
|
|
207
|
+
.ruff_cache/
|
|
208
|
+
|
|
209
|
+
# PyPI configuration file
|
|
210
|
+
.pypirc
|
|
211
|
+
|
|
212
|
+
# Marimo
|
|
213
|
+
marimo/_static/
|
|
214
|
+
marimo/_lsp/
|
|
215
|
+
__marimo__/
|
|
216
|
+
|
|
217
|
+
# Streamlit
|
|
218
|
+
.streamlit/secrets.toml
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
default_language_version:
|
|
2
|
+
python: python3.11
|
|
3
|
+
|
|
4
|
+
repos:
|
|
5
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
6
|
+
rev: v6.0.0
|
|
7
|
+
hooks:
|
|
8
|
+
- id: check-added-large-files
|
|
9
|
+
- id: check-toml
|
|
10
|
+
- id: check-yaml
|
|
11
|
+
args: [ --unsafe ]
|
|
12
|
+
- id: end-of-file-fixer
|
|
13
|
+
- id: trailing-whitespace
|
|
14
|
+
|
|
15
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
16
|
+
# Ruff version.
|
|
17
|
+
rev: v0.15.12
|
|
18
|
+
hooks:
|
|
19
|
+
# Run the linter.
|
|
20
|
+
- id: ruff-check
|
|
21
|
+
args: [ --fix ]
|
|
22
|
+
# Run the formatter.
|
|
23
|
+
- id: ruff-format
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ezequiel Parziale
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-query-filters
|
|
3
|
+
Version: 0.0.1a0
|
|
4
|
+
Summary: Dynamic and declarative query filters for FastAPI, powered by Pydantic v2 and ready for multiple ORMs.
|
|
5
|
+
Author: Ezequiel Parziale
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Requires-Dist: fastapi>=0.110.0
|
|
10
|
+
Requires-Dist: pydantic[email]>=2.0.0
|
|
11
|
+
Requires-Dist: sqlalchemy>=2.0.0
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: httpx; extra == 'dev'
|
|
14
|
+
Requires-Dist: mypy; extra == 'dev'
|
|
15
|
+
Requires-Dist: pre-commit; extra == 'dev'
|
|
16
|
+
Requires-Dist: pytest; extra == 'dev'
|
|
17
|
+
Requires-Dist: ruff; extra == 'dev'
|
|
18
|
+
Requires-Dist: types-setuptools; extra == 'dev'
|
|
19
|
+
Requires-Dist: uvicorn; extra == 'dev'
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# :rocket: fastapi-query-filters
|
|
23
|
+
|
|
24
|
+
Dynamic and declarative query filters for FastAPI, powered by Pydantic v2 and ready for multiple ORMs.
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- 🔍 **Dynamic Filtering**: Generate powerful query filters from Pydantic models automatically
|
|
29
|
+
- 🎯 **Declarative API**: Clean, intuitive syntax for defining filters
|
|
30
|
+
- 🔌 **Multi-ORM Support**: Built-in support for SQLAlchemy with extensibility for other ORMs
|
|
31
|
+
- 📦 **Pydantic v2**: Full integration with Pydantic v2 for robust data validation
|
|
32
|
+
- ⚡ **FastAPI Native**: Seamlessly integrates with FastAPI dependencies
|
|
33
|
+
- 🔤 **Search & Sort**: Support for text search and custom sorting operators
|
|
34
|
+
- 🧪 **Type Safe**: Full type hints and mypy strict mode support
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install git+https://github.com/ezeparziale/fastapi-query-filters.git
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Requirements
|
|
43
|
+
|
|
44
|
+
- Python >= 3.11
|
|
45
|
+
- FastAPI >= 0.110.0
|
|
46
|
+
- Pydantic >= 2.0.0
|
|
47
|
+
- SQLAlchemy >= 2.0.0
|
|
48
|
+
|
|
49
|
+
## Quick Start
|
|
50
|
+
|
|
51
|
+
### 1. Define Your Schema
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from datetime import datetime
|
|
55
|
+
from pydantic import BaseModel, ConfigDict, EmailStr, Field
|
|
56
|
+
|
|
57
|
+
class UserOut(BaseModel):
|
|
58
|
+
id: int = Field(json_schema_extra={"filters": ["eq"]})
|
|
59
|
+
email: EmailStr = Field(json_schema_extra={"filters": ["eq", "icontains"]})
|
|
60
|
+
|
|
61
|
+
model_config = ConfigDict(from_attributes=True)
|
|
62
|
+
|
|
63
|
+
class PostOut(BaseModel):
|
|
64
|
+
id: int = Field(json_schema_extra={"filters": ["eq", "gte", "lte", "in"]})
|
|
65
|
+
title: str = Field(json_schema_extra={"filters": ["eq", "icontains"]})
|
|
66
|
+
created_at: datetime = Field(json_schema_extra={"filters": ["gte", "lte"]})
|
|
67
|
+
is_active: bool = Field(json_schema_extra={"filters": ["eq"]})
|
|
68
|
+
user_id: int = Field(json_schema_extra={"filters": ["eq"]})
|
|
69
|
+
author: UserOut
|
|
70
|
+
|
|
71
|
+
model_config = ConfigDict(from_attributes=True)
|
|
72
|
+
|
|
73
|
+
class FilterConfig:
|
|
74
|
+
search_field = "q"
|
|
75
|
+
sort_field = "sort_by"
|
|
76
|
+
enable_sort = True
|
|
77
|
+
enable_search = True
|
|
78
|
+
prefix = ""
|
|
79
|
+
search_columns = ["title", "description"]
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 2. Use in Your Endpoint
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from fastapi import Depends, FastAPI
|
|
86
|
+
from fastapi_query_filters import FilterDep, FilterValues
|
|
87
|
+
from fastapi_query_filters.orm.sqlalchemy import apply_filters
|
|
88
|
+
|
|
89
|
+
app = FastAPI()
|
|
90
|
+
|
|
91
|
+
@app.get("/posts")
|
|
92
|
+
def list_posts(
|
|
93
|
+
db: Session = Depends(get_db),
|
|
94
|
+
filters: FilterValues = FilterDep(PostOut)
|
|
95
|
+
):
|
|
96
|
+
stmt = select(Post)
|
|
97
|
+
stmt = apply_filters(stmt, Post, filters)
|
|
98
|
+
return db.execute(stmt).scalars().all()
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 3. Query Your API
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
# Filter by field with eq operator
|
|
105
|
+
GET /posts?id__eq=1
|
|
106
|
+
|
|
107
|
+
# Multiple values for in operator
|
|
108
|
+
GET /posts?id__in=1&id__in=2&id__in=3
|
|
109
|
+
|
|
110
|
+
# Use in operator with comma-separated values
|
|
111
|
+
GET /posts?id__in=1,2,3
|
|
112
|
+
|
|
113
|
+
# Filter by field with icontains operator
|
|
114
|
+
GET /posts?title__icontains=python
|
|
115
|
+
|
|
116
|
+
# Multiple filters
|
|
117
|
+
GET /posts?published__eq=true&title__icontains=api
|
|
118
|
+
|
|
119
|
+
# Search across fields
|
|
120
|
+
GET /posts?q=fastapi
|
|
121
|
+
|
|
122
|
+
# Sort results
|
|
123
|
+
GET /posts?sort_by=-created_at,title
|
|
124
|
+
|
|
125
|
+
# Combine filters and search
|
|
126
|
+
GET /posts?id__in=1,2,3&is_active__eq=true&q=fastapi&sort_by=-created_at
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Supported Operators
|
|
130
|
+
|
|
131
|
+
The library supports the following filter operators for different field types:
|
|
132
|
+
|
|
133
|
+
| Operator | Description | Example | Types |
|
|
134
|
+
|----------|-------------|---------|-------|
|
|
135
|
+
| `eq` | Equal | `id__eq=1` | all |
|
|
136
|
+
| `ne` | Not equal | `status__ne=inactive` | all |
|
|
137
|
+
| `gt` | Greater than | `age__gt=18` | int, datetime |
|
|
138
|
+
| `lt` | Less than | `age__lt=65` | int, datetime |
|
|
139
|
+
| `gte` | Greater than or equal | `created_at__gte=2024-01-01` | int, datetime |
|
|
140
|
+
| `lte` | Less than or equal | `price__lte=100` | int, datetime |
|
|
141
|
+
| `in` | In list | `id__in=1,2,3` | all |
|
|
142
|
+
| `not_in` | Not in list | `status__not_in=deleted,archived` | all |
|
|
143
|
+
| `like` | SQL LIKE pattern | `name__like=%john%` | str |
|
|
144
|
+
| `ilike` | SQL ILIKE (case-insensitive) | `email__ilike=%gmail%` | str |
|
|
145
|
+
| `icontains` | Case-insensitive contains | `title__icontains=python` | str |
|
|
146
|
+
|
|
147
|
+
## FilterConfig Configuration
|
|
148
|
+
|
|
149
|
+
Customize filter behavior using the `FilterConfig` nested class in your schema:
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
class PostOut(BaseModel):
|
|
153
|
+
# ... fields ...
|
|
154
|
+
|
|
155
|
+
class FilterConfig:
|
|
156
|
+
# Query parameter name for global search (default: "q")
|
|
157
|
+
search_field = "q"
|
|
158
|
+
|
|
159
|
+
# Query parameter name for sorting (default: "sort_by")
|
|
160
|
+
sort_field = "sort_by"
|
|
161
|
+
|
|
162
|
+
# Global prefix for all filter parameters (default: "")
|
|
163
|
+
prefix = ""
|
|
164
|
+
|
|
165
|
+
# Enable/disable global search functionality (default: True)
|
|
166
|
+
enable_search = True
|
|
167
|
+
|
|
168
|
+
# Enable/disable sorting functionality (default: True)
|
|
169
|
+
enable_sort = True
|
|
170
|
+
|
|
171
|
+
# Columns to search when using global search query
|
|
172
|
+
search_columns = ["title", "description", "content"]
|
|
173
|
+
|
|
174
|
+
# Optional: Pydantic model with virtual/extra filter fields
|
|
175
|
+
extra_filters = PostFilterExtra
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### FilterConfig Parameters Explained
|
|
179
|
+
|
|
180
|
+
- **search_field**: The query parameter name used for global search across multiple columns
|
|
181
|
+
- **sort_field**: The query parameter name used to specify sorting order
|
|
182
|
+
- **prefix**: A prefix prepended to all filter parameter names (e.g., `f_id__eq=1`)
|
|
183
|
+
- **enable_search**: Toggle global search functionality on/off
|
|
184
|
+
- **enable_sort**: Toggle dynamic sorting functionality on/off
|
|
185
|
+
- **search_columns**: List of database columns to search when using the search_field parameter
|
|
186
|
+
- **extra_filters**: A Pydantic model containing additional filter fields from the database that are not included in the main schema output
|
|
187
|
+
|
|
188
|
+
## Advanced Features
|
|
189
|
+
|
|
190
|
+
### Nested Relationship Filtering
|
|
191
|
+
|
|
192
|
+
Filter by nested relationship fields using double underscore notation:
|
|
193
|
+
|
|
194
|
+
```python
|
|
195
|
+
# Filter posts by author's email
|
|
196
|
+
GET /posts?author__email__icontains=example.com
|
|
197
|
+
|
|
198
|
+
# Filter posts by author's age range
|
|
199
|
+
GET /posts?author__age__gte=18&author__age__lte=65
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Dynamic Sorting
|
|
203
|
+
|
|
204
|
+
Sort by multiple fields with ascending/descending order:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
# Sort by created_at (descending), then by title (ascending)
|
|
208
|
+
GET /posts?sort_by=-created_at,title
|
|
209
|
+
|
|
210
|
+
# Sort by multiple fields
|
|
211
|
+
GET /posts?sort_by=-updated_at,id,name
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Field Aliases
|
|
215
|
+
|
|
216
|
+
Use `filter_alias` in `json_schema_extra` to map query parameters to different field names:
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
class PostOut(BaseModel):
|
|
220
|
+
title: str = Field(
|
|
221
|
+
alias="post_title",
|
|
222
|
+
json_schema_extra={
|
|
223
|
+
"filters": ["eq", "icontains"],
|
|
224
|
+
"filter_alias": "post_title",
|
|
225
|
+
},
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
# Query using the alias
|
|
229
|
+
GET /posts?post_title__icontains=python
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Multiple Query Parameter Values
|
|
233
|
+
|
|
234
|
+
Multiple values can be passed in different ways:
|
|
235
|
+
|
|
236
|
+
```bash
|
|
237
|
+
# Multiple eq operators
|
|
238
|
+
GET /posts?id__eq=1&id__eq=2&id__eq=3
|
|
239
|
+
|
|
240
|
+
# Comma-separated values with in operator
|
|
241
|
+
GET /posts?id__in=1,2,3
|
|
242
|
+
|
|
243
|
+
# Combined search and filters
|
|
244
|
+
GET /posts?q=fastapi&is_active__eq=true&author__id__in=1,2,3
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Global Prefix
|
|
248
|
+
|
|
249
|
+
Add a global prefix to all filter parameters for namespace isolation:
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
class FilterConfig:
|
|
253
|
+
prefix = "f_"
|
|
254
|
+
|
|
255
|
+
# Query with prefix
|
|
256
|
+
GET /posts?f_id__eq=1&f_title__icontains=api
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Extra Filters (Virtual Fields)
|
|
260
|
+
|
|
261
|
+
Define additional filter fields that exist in the database but are not included in your main response schema:
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
class PostFilterExtra(BaseModel):
|
|
265
|
+
# Database column available for filtering but not in PostOut response
|
|
266
|
+
author__age: int | None = Field(
|
|
267
|
+
default=None,
|
|
268
|
+
json_schema_extra={"filters": ["gte", "lte", "in"]}
|
|
269
|
+
)
|
|
270
|
+
# Another database field not exposed in the API response
|
|
271
|
+
status: str | None = Field(
|
|
272
|
+
default=None,
|
|
273
|
+
json_schema_extra={"filters": ["eq", "in"]}
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
class PostOut(BaseModel):
|
|
277
|
+
# ... fields exposed in response ...
|
|
278
|
+
|
|
279
|
+
class FilterConfig:
|
|
280
|
+
extra_filters = PostFilterExtra
|
|
281
|
+
|
|
282
|
+
# Query using extra filter fields
|
|
283
|
+
GET /posts?author__age__gte=18
|
|
284
|
+
GET /posts?status__eq=draft
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Documentation
|
|
288
|
+
|
|
289
|
+
For detailed documentation and examples, see the [examples/](./examples/) directory.
|
|
290
|
+
|
|
291
|
+
## License
|
|
292
|
+
|
|
293
|
+
MIT License - see [LICENSE](./LICENSE) file for details.
|
|
294
|
+
|
|
295
|
+
## Author
|
|
296
|
+
|
|
297
|
+
Ezequiel Parziale
|