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

Commit

Permalink
Calculate dist relative to the queryset model
Browse files Browse the repository at this point in the history
Previously the distribution was calculated with respect to itself and
did not take handle the case when the queryset was based on a different
model.

Signed-off-by: Byron Ruth <b@devel.io>
  • Loading branch information
bruth committed Dec 8, 2016
1 parent a68f7ac commit 05cdf7c
Showing 1 changed file with 8 additions and 4 deletions.
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

0 comments on commit 05cdf7c

Please sign in to comment.