GROUP_CONCAT等效于Django [英] GROUP_CONCAT equivalent in Django
问题描述
id |类型|名称
-----------------
0 |苹果|富士
1 |苹果| mac
2 |橙色| navel
我的目标是最终提出不同的类型的计数
和名称的逗号分隔列表
:
apple,2,fuji,mac
orange,1,navel
这可以很容易地在MySQL中使用 GROUP_CONCAT
完成,但是我遇到Django等效问题。这是我到目前为止,但我错过了 GROUP_CONCAT
的东西:
query_set = Fruits.objects.values('type')。annotate(count = Count('type'))。order_by(' - count')
如果可能,我想避免使用原始SQL查询。
任何帮助将不胜感激! >
谢谢! =)
Django ORM不支持此功能;如果您不想使用原始SQL,那么您需要组合并加入。
Say I have the following table called fruits
:
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 types
and a comma-delimited list of the names
:
apple, 2, "fuji,mac"
orange, 1, "navel"
This can be easily done with GROUP_CONCAT
in MySQL but I'm having trouble with the Django equivalent. This is what I have so far but I am missing the GROUP_CONCAT
stuff:
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! =)
The Django ORM does not support this; if you don't want to use raw SQL then you'll need to group and join.
这篇关于GROUP_CONCAT等效于Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!