Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django most efficient way to count same field values in a query

Lets say if I have a model that has lots of fields, but I only care about a charfield. Lets say that charfield can be anything so I don't know the possible values, but I know that the values frequently overlap. So I could have 20 objects with "abc" and 10 objects with "xyz" or I could have 50 objects with "def" and 80 with "stu" and i have 40000 with no overlap which I really don't care about.

How do I count the objects efficiently? What I would like returned is something like:

{'abc': 20, 'xyz':10, 'other': 10,000}

or something like that, w/o making a ton of SQL calls.

EDIT:

I dont know if anyone will see this since I am editing it kind of late, but...

I have this model:

 class Action(models.Model):     author = models.CharField(max_length=255)     purl = models.CharField(max_length=255, null=True) 

and from the answers, I have done this:

 groups = Action.objects.filter(author='James').values('purl').annotate(count=Count('purl')) 

but...

this is what groups is:

 {"purl": "waka"},{"purl": "waka"},{"purl": "waka"},{"purl": "waka"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "mora"},{"purl": "lora"} 

(I just filled purl with dummy values)

what I want is

 {'waka': 4, 'mora': 5, 'lora': 1} 

Hopefully someone will see this edit...

EDIT 2:

Apparently my database (BigTable) does not support the aggregate functions of Django and this is why I have been having all the problems.

like image 968
DantheMan Avatar asked Aug 31 '10 06:08

DantheMan


People also ask

What is Select_related in Django?

Using select_related() Django offers a QuerySet method called select_related() that allows you to retrieve related objects for one-to-many relationships. This translates to a single, more complex QuerySet, but you avoid additional queries when accessing the related objects.

What is QuerySet in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.

How does Django count data?

You can either use Python's len() or use the count() method on any queryset depending on your requirements. Also note, using len() will evaluate the queryset so it's always feasible to use the provided count() method. You should also go through the QuerySet API Documentation for more information.


2 Answers

You want something similar to "count ... group by". You can do this with the aggregation features of django's ORM:

from django.db.models import Count  fieldname = 'myCharField' MyModel.objects.values(fieldname)     .order_by(fieldname)     .annotate(the_count=Count(fieldname)) 

Previous questions on this subject:

  • How to query as GROUP BY in django?
  • Django equivalent of COUNT with GROUP BY
like image 130
beerbajay Avatar answered Oct 14 '22 05:10

beerbajay


This is called aggregation, and Django supports it directly.

You can get your exact output by filtering the values you want to count, getting the list of values, and counting them, all in one set of database calls:

from django.db.models import Count MyModel.objects.filter(myfield__in=('abc', 'xyz')).\         values('myfield').annotate(Count('myfield')) 
like image 20
Daniel Roseman Avatar answered Oct 14 '22 05:10

Daniel Roseman