Skip to content

Instantly share code, notes, and snippets.

@iandmyhand
Last active January 8, 2023 08:10
Show Gist options
  • Save iandmyhand/b2c32311715113e8c470932a053a6732 to your computer and use it in GitHub Desktop.
Save iandmyhand/b2c32311715113e8c470932a053a6732 to your computer and use it in GitHub Desktop.
Django ORM function to sum absolute values.
from django.db.models import Sum
class AbsoluteSum(Sum):
name = 'AbsoluteSum'
template = '%(function)s(%(absolute)s(%(expressions)s))'
def __init__(self, expression, **extra):
super(AbsoluteSum, self).__init__(
expression, absolute='ABS ', output_field=IntegerField(), **extra)
def __repr__(self):
return "SUM(ABS(%s))".format(
self.arg_joiner.join(str(arg) for arg in self.source_expressions)
)
@LowerDeez
Copy link

Nice

@skyl
Copy link

skyl commented Jan 8, 2023

still works ;d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment