2015-11-01 78 views
1

我有兩種型號ProductOfferOfferProduct有關並且包含'price'字段。 我可以得到最小值: Product.objects.get(pk=1).offer_set.aggregate(Min('price'))如何統計有多少記錄在現場有最小值? (Django)

但是我怎麼能得到這個值在列中出現多少次?

有我的解決方案: Product.objects.get(pk=1).offer_set.values('price').order_by().annotate(Count('‌​price'))然後,我可以得到返回的列表

的第一個元素是否有更好的解決辦法?

回答

2

你可以這樣做,沒有試過;

from django.db.models import Count 

Product.objects.get(pk=1).offer_set.all().values('price').annotate(total=Count('price')).order_by('price') 
相關問題