Allen Liu Allen Liu - 10 months ago 155
MySQL Question

GROUP_CONCAT equivalent in Django

Say I have the following table called


id | type | name
0 | apple | fuji
1 | apple | mac
2 | orange | navel

My goal is to ultimately come up with a count of the different
and a comma-delimited list of the

apple, 2, "fuji,mac"
orange, 1, "navel"

This can be easily done with
in MySQL but I'm having trouble with the Django equivalent. This is what I have so far but I am missing the

query_set = Fruits.objects.values('type').annotate(count=Count('type')).order_by('-count')

I would like to avoid using raw SQL queries if possible.

Any help would be greatly appreciated!

Thanks! =)

Answer Source

The Django ORM does not support this; if you don't want to use raw SQL then you'll need to group and join.