私はそのようなテーブルを持っています
id p_id number
1 1 12
2 1 13
3 2 14
4 2 15
私のビューでそのようなアイテムを取得する方法は、p_id でグループ化し、グループから数値を取得することを意味します
for p_id 1 returns count 2 and 12,13
for p_id 2 returns count 2 and 14,15
私はそのようなテーブルを持っています
id p_id number
1 1 12
2 1 13
3 2 14
4 2 15
私のビューでそのようなアイテムを取得する方法は、p_id でグループ化し、グループから数値を取得することを意味します
for p_id 1 returns count 2 and 12,13
for p_id 2 returns count 2 and 14,15
and を使用values()
しannotate()
てグループ化できるため、次のようになります。
from django.db.models import Count
pid_group = YourModel.objects.values('p_id').annotate(ncount=Count('number'))
その後、次のpid_group
ような辞書のリストを取得します。
[{'p_id': 1, 'ncount': 2}, {'p_id': 2, 'ncount': 2}]
次に、数値を簡単に取得できます。
for pid in pid_group:
number_values = YourModel.objects.filter(p_id=pid.get('p_id'))
それが役に立てば幸い。