Commits

Luke Plant committed 09cec80

Pulled out 'value_counts' query functionality into function in queries module

Comments (0)

Files changed (2)

django_easyfilters/filters.py

 from django.utils.datastructures import SortedDict
 from django.utils.dates import MONTHS
 from django.utils.text import capfirst
-from django_easyfilters.queries import date_aggregation
+from django_easyfilters.queries import date_aggregation, value_counts
 
 try:
     from collections import namedtuple
         The order is the underlying order produced by sorting ascending on the
         DB field.
         """
-        values_counts = qs.values_list(self.field).order_by(self.field).annotate(models.Count(self.field))
-
-        count_dict = SortedDict()
-        for val, count in values_counts:
-            count_dict[val] = count
-        return count_dict
+        return value_counts(qs, self.field)
 
 
 class DrillDownMixin(object):
 
         # Now get counts:
         field_name = fkey_other.name
-        values_counts = m2m_objs.values_list(field_name).order_by(field_name).annotate(models.Count(field_name))
-
-        count_dict = SortedDict()
-        for val, count in values_counts:
-            count_dict[val] = count
-
-        return count_dict
+        return value_counts(m2m_objs, field_name)
 
     def get_choices_add(self, qs):
         count_dict = self.get_values_counts(qs)

django_easyfilters/queries.py

+from django.db import models
+from django.db.models.sql.compiler import SQLCompiler
+from django.db.models.sql.constants import MULTI
 from django.db.models.sql.datastructures import Date
 from django.db.models.sql.subqueries import AggregateQuery
-from django.db.models.sql.compiler import SQLCompiler
-from django.db.models.sql.constants import MULTI
+from django.utils.datastructures import SortedDict
 
 
 # Some fairly brittle, low level stuff, to get the aggregation
     query = DateAggregateQuery(date_qs.model)
     query.add_subquery(date_q, date_qs.db)
     return query.get_counts(date_qs.db)
+
+
+def value_counts(qs, fieldname):
+    """
+    Performs a simple query returning the count of each value of
+    the field 'fieldname' in the QuerySet, returning the results
+    as a SortedDict of value: count
+    """
+    values_counts = qs.values_list(fieldname).order_by(fieldname).annotate(models.Count(fieldname))
+    count_dict = SortedDict()
+    for val, count in values_counts:
+        count_dict[val] = count
+    return count_dict
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.