Skip to content

Commit

Permalink
Merge pull request #51 from Aristotle-Metadata-Enterprises/lookup
Browse files Browse the repository at this point in the history
Wrap custom Django SQL in a `Func` so we can treat ComplexLookup the same as other DB functions
  • Loading branch information
s-i-l-k-e committed Dec 20, 2023
2 parents 9336743 + 81b2f9b commit 3cde94c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions data_interrogator/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ def as_postgresql(self, compiler, connection):
return super().as_sql(compiler, connection)


def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField()):
expression = Coalesce(
class ComplexLookup(Func):
"""Wrap custom Django SQL in a `Func` so we can treat ComplexLookup the same as other DB functions"""
function = ''
template = "%(expressions)s"
arity = 1

def __init__(self, lookup_field, condition, lookup_value, output_field=TextField(), **extra):
expression = Coalesce(
GroupConcat(
Cast(
Case(
Expand All @@ -46,8 +52,8 @@ def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField(
)
),
Value("")
)
return expression
)
super().__init__(expression, **extra)


class SumIf(Sum):
Expand All @@ -64,7 +70,7 @@ def __init__(self, field, condition=None, output_field=DecimalField(), **lookups
case = Case(
When(
condition, then=field
),
),
default=0,
output_field=output_field
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-data-interrogator"
version = "0.3.2"
version = "0.3.3"
description = "A suite of interactive table builder utilities that create reports using efficient SQL queries"
authors = ["Aristotle Metadata Enterprises"]
license = "BSD-3-Clause"
Expand Down

0 comments on commit 3cde94c

Please sign in to comment.