Last active
January 8, 2023 08:10
-
-
Save iandmyhand/b2c32311715113e8c470932a053a6732 to your computer and use it in GitHub Desktop.
Django ORM function to sum absolute values.
This file contains 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) | |
) |
very good
Nice
still works ;d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'test' table
'Test' model
Usage
Result