Skip to main content

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) and JsonExtract for JSON fields
  • DeepParser – Parses dynamic filter expressions from string parameters (include, exclude, order, distinct) to build Django Q objects
  • SubqueriesSumSubquery, 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()
    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


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.1.tar.gz (29.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_deep-0.1.1-py3-none-any.whl (32.8 kB view details)

Uploaded Python 3

File details

Details for the file django_deep-0.1.1.tar.gz.

File metadata

  • Download URL: django_deep-0.1.1.tar.gz
  • Upload date:
  • Size: 29.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_deep-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f0845a9ed9c0e7f4b26cc73ed533f342f46996abcb74fe64170ec736148f6467
MD5 985cb7b0bb7bf4793294578e9136a949
BLAKE2b-256 42f926fc7e6dedfd012fd16d7b3190d34b047933e95c57ae23261f1c04aefa7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_deep-0.1.1.tar.gz:

Publisher: release.yml on octolo/django-deep

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_deep-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_deep-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 32.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_deep-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f6b0ef1b2e19b06111e81ba8f37049d330f5fed0c78cd50b06183fd62f76b18
MD5 a6922f722bfe6171a288bea815aabfb8
BLAKE2b-256 369058a0a50b3435596d2011747c4e279e70deb261a1101a15c2887729ddfa65

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_deep-0.1.1-py3-none-any.whl:

Publisher: release.yml on octolo/django-deep

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page