Skip to content

Instantly share code, notes, and snippets.

@rdmurphy
rdmurphy / aggregates.py
Created May 5, 2018 16:14
PostgreSQL specific Django Median aggregation function
from django.db.models import Aggregate, FloatField
class Median(Aggregate):
function = 'PERCENTILE_CONT'
name = 'median'
output_field = FloatField()
template = '%(function)s(0.5) WITHIN GROUP (ORDER BY %(expressions)s)'