diff options
-rw-r--r-- | db.py | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -0,0 +1,33 @@ +from django.db.models import Manager +from django.db.models.query import QuerySet +from django.db.models.sql.query import Query + + +class GroupedQuery(Query): + def add_grouping(self, *grouping): + if self.group_by is None: + self.group_by = [] + + if isinstance(self.group_by, list): + self.group_by.extend(grouping) + + def clear_grouping(self): + self.group_by = None + + +class GroupedQuerySet(QuerySet): + def __init__(self, model=None, query=None, using=None, hints=None): + super(BetterQuerySet, self).__init__(model, query, using, hints) + self.query = query or BetterQuery(self.model) + + def group_by(self, *field_names): + obj = self._clone() + obj.query.clear_grouping() + obj.query.add_grouping(*field_names) + return obj + + +class GroupedManager(Manager): + def __init__(self): + super(GroupedManager, self).__init__() + self._queryset_class = GroupedQuerySet |