diff options
| author | Alban Gruin | 2017-01-27 21:05:33 +0100 | 
|---|---|---|
| committer | Alban Gruin | 2017-01-27 21:05:33 +0100 | 
| commit | 0f2525a12ea0ed03d3d33a48b112d3d80a622b75 (patch) | |
| tree | 0b3d8cf9d292630f9c192981824e9f6b1512f506 | |
| parent | 98a7494b6f930490e62cfc134f23d69b32203977 (diff) | |
Travail initial sur l'implémentation d'un 'group by' un minimum sain pour l'orm de django
| -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  | 
