Skip to content
This repository has been archived by the owner on Jan 18, 2020. It is now read-only.

Calculate dist relative to the queryset model #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions avocado/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.db.models.fields import FieldDoesNotExist
from django.db.models.signals import post_save, pre_delete
from django.core.exceptions import ValidationError
from modeltree.tree import trees
from avocado.core import utils
from avocado.core.structures import ChoicesDict
from avocado.core.models import Base, BasePlural, PublishArchiveMixin
Expand Down Expand Up @@ -568,10 +569,13 @@ def dist(self, queryset=None):
if queryset is None:
queryset = self.model.objects.all()

queryset = queryset.values(self.value_field.name)\
.annotate(cnt=Count(self.value_field.name))\
.values_list(self.value_field.name, 'cnt')\
.order_by(self.value_field.name)
tree = trees[queryset.model]
group_by = tree.query_string_for_field(self.value_field)

queryset = queryset.values(group_by)\
.annotate(cnt=Count('pk', distinct=True))\
.values_list(group_by, 'cnt')\
.order_by(group_by)

return tuple(queryset)

Expand Down