Skip to content

Instantly share code, notes, and snippets.

@zhu327
Created November 14, 2016 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhu327/0a9b3270e792080b97264061b5eaaf7f to your computer and use it in GitHub Desktop.
Save zhu327/0a9b3270e792080b97264061b5eaaf7f to your computer and use it in GitHub Desktop.
simple django custom aggregation
from django.db.models import Aggregate
from django.db.models.sql import aggregates
def custom_aggregation(select_query):
class SqlAggregate(aggregates.Aggregate):
sql_function = ''
sql_template = select_query
class VisitorRate(Aggregate):
sql = SqlAggregate
def add_to_query(self, query, alias, col, source, is_summary):
aggregate = self.sql(col,
source=source,
is_summary=is_summary,
**self.extra)
query.aggregates[alias] = aggregate
return VisitorRate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment