aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/db.py b/db.py
new file mode 100644
index 0000000..d8f92ef
--- /dev/null
+++ b/db.py
@@ -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