在Django中查询前x个元素 [英] Query for top x elements in Django
本文介绍了在Django中查询前x个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个模型,
class JobTitle(models.Model):
name = models.CharField(max_length=1000)
class Employer(models.Model):
jobtitle = models.ForeignKey(JobTitle,unique=False,null=True)
如您所见,一个雇主可能有很多职位。我尝试进行查询以获取职位数量最多的前5名雇主
As you see, one employer may have many jobtitles. I try to make a query to get top 5 employers whose number of job titles is maximum
我如何才能做到这就是Django?
How can I achive this is Django ?
谢谢
推荐答案
Employer.objects.values('id').annotate(jobtitle_count=Count('jobtitle')).order_by('-jobtitle_count')[:5]
这篇关于在Django中查询前x个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文