I have a model that looks like:
class Foo(models.Model):
bar = models.CharField(...)
baz = models.CharField(...)
Foo(bar="a", baz="x")
Foo(bar="a", baz="x")
Foo(bar="a", baz="y")
Foo(bar="b", baz="y")
And I want a queryset that will return the largest count of unique (bar, baz) pairs for each bar:
[
{"bar": "a", "baz": "x", "count": 2},
{"bar": "b", "baz": "y", "count": 1},
]
Ideally this will all be done within a query, I've tried combinations of distinct, aggregate, annotate, to no avail and can't see how to do it other than run raw SQL which I want to avoid.
I'm using the PostgreSQL as my database backend.