Python 如何在django中获得外键数最多的行?

Python 如何在django中获得外键数最多的行?,python,django,django-models,Python,Django,Django Models,我有一个Writer模型/表格和一个文章模型,如: class Writer(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) pic = models.URLField() start_date = models.DateField('date of start', blank=True, null=True) about = models.TextField(m

我有一个Writer模型/表格和一个文章模型,如:

class Writer(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    pic = models.URLField()
    start_date = models.DateField('date of start', blank=True, null=True)
    about = models.TextField(max_length=300)

class Article(models.Model):
    writer = models.ForeignKey(Writer)
    name = models.CharField(max_length=100)
    content = models.DecimalField(max_digits=10, decimal_places=2)
    date = models.DateField(auto_now=False, auto_now_add=True)
一篇文章只能有一个作者,但是一个作者可以有很多文章。我可以让所有的作者都有

Writer.objects.all()
但我想让作者写大多数文章。我该怎么做

返回一个整数,表示数据库中的对象数 匹配查询集。count方法从不引发异常

例如:

返回数据库中的条目总数

返回标题包含“Lennon”的条目数

使用此计数方法,您可以通过执行Writer.article\u set.count来计数相关的文章集,并获取文章最多的对象

higest_count = Writer.objects.annotate(num_articles=Count('article')).order_by('-num_articles')[0].num_articles
print(higest_count) #this will print highest count
articles=Writer.objects.annotatenum\u articles=Count'article'。按'-num\u articles'排序阅读聚合函数
Entry.objects.filter(headline__contains='Lennon').count()
higest_count = Writer.objects.annotate(num_articles=Count('article')).order_by('-num_articles')[0].num_articles
print(higest_count) #this will print highest count