django-aggregation

Sum together list of F expressions

Sum together list of F expressions Question: Is there a way to specify (in an annotation or aggregation) that a sequence of F expressions should be summed together without manually typing out F("first_prop") + F("second_prop") + …? I want something similar to how python’s sum() function allows you to pass an iterable and get the …

Total answers: 1

Django aggregate sum for each user

Django aggregate sum for each user Question: I’m creating a budget application and wanted to find the sum of each user who’s signed in. Right now, I’m using function-based views and I sum up the expenses with Post.objects.aggregate(sum = Sum(‘expense’)) The issue with this is it sums up everyone in the database but I wanted …

Total answers: 3

Django aggregation. How to render its value into the template?

Django aggregation. How to render its value into the template? Question: I have came across on one of the weirdest problem which I literally can’t understand. Well I was following This, but nothing helped. As the question says, I just want to render the result into the template. Below is my code. Views.py … invoice …

Total answers: 2

Using Subquery to annotate a Count

Using Subquery to annotate a Count Question: Please help me I’ve been stuck on this for way too long 🙁 What I want to do: I have these two models: class Specialization(models.Model): name = models.CharField(“name”, max_length=64) class Doctor(models.Model): name = models.CharField(“name”, max_length=128) # … specialization = models.ForeignKey(Specialization) I would like to annotate all specializations in …

Total answers: 2

How to filter objects for count annotation in Django?

How to filter objects for count annotation in Django? Question: Consider simple Django models Event and Participant: class Event(models.Model): title = models.CharField(max_length=100) class Participant(models.Model): event = models.ForeignKey(Event, db_index=True) is_paid = models.BooleanField(default=False, db_index=True) It’s easy to annotate events query with total number of participants: events = Event.objects.all().annotate(participants=models.Count(‘participant’)) How to annotate with count of participants filtered by …

Total answers: 6

Django equivalent of COUNT with GROUP BY

Django equivalent of COUNT with GROUP BY Question: I know Django 1.1 has some new aggregation methods. However I couldn’t figure out equivalent of the following query: SELECT player_type, COUNT(*) FROM players GROUP BY player_type; Is it possible with Django 1.1’s Model Query API or should I just use plain SQL? Asked By: Imran || …

Total answers: 2