Created
November 14, 2016 09:55
-
-
Save zhu327/0a9b3270e792080b97264061b5eaaf7f to your computer and use it in GitHub Desktop.
simple django custom aggregation
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 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