From 0f2525a12ea0ed03d3d33a48b112d3d80a622b75 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Fri, 27 Jan 2017 21:05:33 +0100 Subject: Travail initial sur l'implémentation d'un 'group by' un minimum sain pour l'orm de django --- db.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 db.py 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 -- cgit v1.2.1