blob: d8f92ef179ba5c72e8bd7f0ccdffa0cb3062ea99 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
|