Skip to content

Instantly share code, notes, and snippets.

@jourdanrodrigues
Created March 22, 2017 17:46
Show Gist options
  • Save jourdanrodrigues/e67ecc8839f47a6579e7769937a1d3a7 to your computer and use it in GitHub Desktop.
Save jourdanrodrigues/e67ecc8839f47a6579e7769937a1d3a7 to your computer and use it in GitHub Desktop.
from django.db.models import Aggregate
class Round(Aggregate):
"""
Solution source for future reference: http://stackoverflow.com/a/34622417/4694834
"""
function = 'ROUND'
template = '%(function)s(CAST(%(expressions)s AS NUMERIC), {decimals})'
name = 'Round'
def __init__(self, data, decimals=2):
super(Round, self).__init__(data)
self.template = self.template.format(decimals=decimals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment