Django ORM function to sum absolute values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment