2

Let's say I have the following models:

class Conversation(Model):
  ...

class Message(Model):
  conversation = models.ForeignKey(Conversation)

The following code counts the number of messages for each conversation:

Conversation.objects \
            .filter(...) \
            .annotate(size=Count("message")) \
            .values("size")

and returns a query set of {'size': 0}, {'size': 1}, {'size': 0}, {'size': 0} etc.

But how do I aggregate over the conversation sizes? That is, obtaining a result something like this: (0, 3), (1, 1), where the first element in each pair is the conversation size and the second element is the number of conversations of this size?

Petter
  • 37,121
  • 7
  • 47
  • 62

0 Answers0