diff --git a/data_interrogator/db.py b/data_interrogator/db.py index 06533fe..2605a0d 100755 --- a/data_interrogator/db.py +++ b/data_interrogator/db.py @@ -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( @@ -46,8 +52,8 @@ def ComplexLookup(lookup_field, condition, lookup_value, output_field=TextField( ) ), Value("") - ) - return expression + ) + super().__init__(expression, **extra) class SumIf(Sum): @@ -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 ) diff --git a/pyproject.toml b/pyproject.toml index b17e354..f3c43cc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"