Django Complex Filter
A powerful and flexible declarative filter for Django ORM that enables complex query construction through a simple JSON-based API. Perfect for building advanced filtering capabilities in your Django REST Framework applications.
Features
- Advanced Filtering: Complex AND/OR operations with nested conditions
- Declarative Syntax: Simple JSON-based query structure
- Dynamic Values: Support for computed values and server-side calculations
- Related Model Queries: Efficient subquery handling for related models
- Extensible: Easy to add custom operators and value functions
- Type Safe: Built-in operator validation
- DRF Integration: Seamless integration with Django REST Framework
Installation
pip install drf-complex-filter
Quick Start
- Add
ComplexQueryFilterto your ViewSet:
from drf_complex_filter.filters import ComplexQueryFilter
class UserViewSet(ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
filter_backends = [ComplexQueryFilter]
- Make API requests with complex filters:
# Simple equality filter
GET /users?filters={"type":"operator","data":{"attribute":"first_name","operator":"=","value":"John"}}
# Complex AND condition
GET /users?filters={"type":"and","data":[
{"type":"operator","data":{"attribute":"age","operator":">","value":18}},
{"type":"operator","data":{"attribute":"is_active","operator":"=","value":true}}
]}
Filter Types
1. Simple Operator
{
"type": "operator",
"data": {
"attribute": "field_name",
"operator": "=",
"value": "value_to_compare"
}
}
2. AND Operator
{
"type": "and",
"data": [
# List of operators to combine with AND
]
}
3. OR Operator
{
"type": "or",
"data": [
# List of operators to combine with OR
]
}
Available Operators
| Operator | Description | Symbol |
|---|---|---|
| Is | Equality | = |
| Is not | Inequality | != |
| Contains | Case-insensitive contains | * |
| Not contains | Case-insensitive not contains | ! |
| Greater | Greater than | > |
| Greater or equal | Greater than or equal | >= |
| Less | Less than | < |
| Less or equal | Less than or equal | <= |
| In | Value in list | in |
| Not in | Value not in list | not_in |
| Current user | Current authenticated user | me |
| Not current user | Not current authenticated user | not_me |
Advanced Features
Custom Operators
- Create your operator class:
class CustomOperators:
def get_operators(self):
return {
"custom_op": lambda f, v, r, m: Q(**{f"{f}__custom": v}),
}
- Register in settings:
COMPLEX_FILTER_SETTINGS = {
"COMPARISON_CLASSES": [
"drf_complex_filter.comparisons.CommonComparison",
"path.to.CustomOperators",
],
}
Dynamic Values
- Create value functions:
class CustomFunctions:
def get_functions(self):
return {
"current_time": lambda request, model: timezone.now(),
}
- Register in settings:
COMPLEX_FILTER_SETTINGS = {
"VALUE_FUNCTIONS": [
"drf_complex_filter.functions.DateFunctions",
"path.to.CustomFunctions",
],
}
- Use in filters:
{
"type": "operator",
"data": {
"attribute": "created_at",
"operator": ">",
"value": {
"func": "current_time",
"kwargs": {}
}
}
}
Related Model Queries
Use ModelName___ prefix for efficient subqueries:
{
"type": "operator",
"data": {
"attribute": "Profile___is_verified",
"operator": "=",
"value": true
}
}
Requirements
- Python >= 3.6
- Django >= 3.0.0
- Django REST Framework
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for drf-complex-filter 2.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| drf_complex_filter-2.0.2.tar.gz | 18.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_complex_filter-2.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.9 kB
Release files / drf_complex_filter-2.0.2.tar.gz
| Download URL | drf_complex_filter-2.0.2.tar.gz |
|---|---|
| Size | 18.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
22d5ff027468835edef4c727bdd594aa62469bb3498fdfa3b52b3f66ee7373ac
|
|
BLAKE2b-256 checksum How to use checksums |
ca7d6644543dd0c5136843b8da679a886c422a9351feb1ce1c89156457750ace
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.21
|
Release files / drf_complex_filter-2.0.2-py3-none-any.whl
| Download URL | drf_complex_filter-2.0.2-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc5a35b3886de55eed6a53786913389e2b111e0b4ee263b63caacddd42d1d4f2
|
|
BLAKE2b-256 checksum How to use checksums |
48c7d9fc2cc76c19343e08b8bd8187309b81ea0f27a95d765b955fdd8e848b40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.21
|
Security for drf-complex-filter 2.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.