Django Enhanced Expression Parser
Project description
django-deep
Django library for advanced SQL expressions: subqueries (SUM, COUNT, JsonAgg, ConcatValues), JSON extraction, and dynamic filter parsing.
Features
- DeepManager – Custom manager with subquery helpers (
get_sum_sub_query,get_count_sub_query,get_json_agg_sub_query,get_concat_values_sub_query,get_method_from_sub_query) andJsonExtractfor JSON fields - DeepParser – Parses dynamic filter expressions from string parameters (include, exclude, order, distinct) to build Django Q objects
- Subqueries –
SumSubquery,CountSubquery,JsonAggSubquery,ConcatValuesSubquery,MethodFromSubquery,ExtractValueSubquery - JsonExtract – Database-agnostic JSON field extraction (PostgreSQL, MySQL, SQLite, Oracle)
- MemoryQuerySet – In-memory queryset for testing
Installation
pip install django-deep
Quick start
from django.db import models
from django.db.models import OuterRef
from django_deep import DeepManager
class Order(models.Model):
customer = models.ForeignKey("Customer", on_delete=models.CASCADE)
total = models.DecimalField(max_digits=10, decimal_places=2)
class Customer(models.Model):
name = models.CharField(max_length=255)
objects = DeepManager()
# Annotate with subqueries
Customer.objects.annotate(
order_count=Customer.objects.get_count_sub_query(
Order.objects.filter(customer_id=OuterRef("pk"))
),
order_sum=Customer.objects.get_sum_sub_query(
Order.objects.filter(customer_id=OuterRef("pk")), sum_field="total"
),
)
Subqueries
from django.db.models import OuterRef
from django_deep import DeepManager
# SumSubquery
qs.annotate(total=manager.get_sum_sub_query(Order.objects.filter(...), "total"))
# CountSubquery
qs.annotate(cnt=manager.get_count_sub_query(Order.objects.filter(...)))
# JsonAggSubquery (PostgreSQL, MySQL, SQLite, Oracle)
qs.annotate(items=manager.get_json_agg_sub_query(RelatedModel.objects.filter(...)))
JsonExtract
from django_deep.functions import JsonExtract
# Extract from JSONField
Model.objects.annotate(
value=JsonExtract("metadata", "path__to__field")
)
Dynamic filters (DeepParser)
Parse include/exclude/order from request params:
parser = DeepParser(queryset, params={"i": "field__filter:value", "o": "name"})
compiled = parser.execute(params.get("i"))
if compiled:
qs = queryset.filter(compiled)
qs = parser.get_queryset()
Development
pip install -e ".[dev]"
pytest
ruff check .
ruff format .
License
GPL-3.0-or-later — see the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django_deep-0.1.0.tar.gz
(34.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_deep-0.1.0.tar.gz.
File metadata
- Download URL: django_deep-0.1.0.tar.gz
- Upload date:
- Size: 34.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17c4e2cb7733ee0c3a97bf31b3a3eb9852d412bfa871b90c593e63943871c93a
|
|
| MD5 |
b43eec40a771f351215ce9df25333316
|
|
| BLAKE2b-256 |
6e210696179a2d7e253a3f23bcdc0fc79e31c2800012fd840666d05dd85b4d85
|
File details
Details for the file django_deep-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_deep-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1438990041b2641f5628e6b924200e8e94fef402ed67c1f680dbf79a9917a3c5
|
|
| MD5 |
0fb80a2003b3cb91c030b8b22f72cd37
|
|
| BLAKE2b-256 |
47c204ad2ea09f2221e406cceee28d9e38b36aac9c82d05c9973f6e72b65e1d0
|