Python 如何编写Django查询,其中的条件是相关项的计数?

Python 如何编写Django查询,其中的条件是相关项的计数?,python,django,python-3.x,count,foreign-keys,Python,Django,Python 3.x,Count,Foreign Keys,我正在使用Django和Python 3.7。我有这两个模型,通过外键相互关联 class Website(models.Model): objects = WebsiteManager() path = models.CharField(max_length=100) class Article(models.Model): website = models.ForeignKey(Website, on_delete=models.CASCADE, related

我正在使用Django和Python 3.7。我有这两个模型,通过外键相互关联

class Website(models.Model):
    objects = WebsiteManager()
    path = models.CharField(max_length=100)



class Article(models.Model):
    website = models.ForeignKey(Website, on_delete=models.CASCADE, related_name='articlesite')
    title = models.TextField(default='', null=False)
    url = models.TextField(default='', null=False)
    created_on = models.DateTimeField(db_index=True, default=datetime.now)
我想编写一个Django查询,返回链接了100多篇文章的网站。在PostGres中,我可以编写此查询

select w.id, count(*) FROM website w, article a where w.id = a.website_id group by w.id;
但我不清楚如何使用Django查询实现这一点。如何编写条件为COUNT函数的查询

编辑:

我修改了查询以添加条件

qset = Website.objects.annotate(articlesite_count=Count('articlesite')).filter(
                         articlesite__edited_date__null=True,
                         articlesite_count__gte=100)
但现在这导致了错误

Unsupported lookup 'null' for DateTimeField or join on the field not permitted.

从django.db.models导入计数
Website.objects.annotation(articlesite\u count=count('articlesite')).filter(articlesite\u count\u gte=100)

更新-1
从django.db.models导入计数
Website.objects.filter(articlesite\uuuu edited\u date\uuuuu isnull=True)。注释(
articlesite\u count=count('articlesite')。筛选器(
articlesite_count_gte=100)

从django.db.models导入计数
Website.objects.annotation(articlesite\u count=count('articlesite')).filter(articlesite\u count\u gte=100)

更新-1
从django.db.models导入计数
Website.objects.filter(articlesite\uuuu edited\u date\uuuuu isnull=True)。注释(
articlesite\u count=count('articlesite')。筛选器(

articlesite\u count\u gte=100)
感谢您的支持。也许我应该问一个单独的问题,但是你知道我如何在我的“过滤器”中添加一个附加条件吗?我添加了一个额外的条件(在我的问题编辑中指定),但现在这会导致“DateTimeField的不支持的查找'null'或字段上的联接不允许”。错误。您可以添加该日期字段吗?您所说的
高文章计数是什么意思?
?您好@JPG,您知道了--我在问题中将日期字段定义添加到了模型中。“high_article_count”是一个数字,但为了清楚起见,我刚刚更新了我的问题,将数字包含进来,而不是更奇怪的变量名。谢谢。也许我应该问一个单独的问题,但是你知道我如何在我的“过滤器”中添加一个附加条件吗?我添加了一个额外的条件(在我的问题编辑中指定),但现在这会导致“DateTimeField的不支持的查找'null'或字段上的联接不允许”。错误。您可以添加该日期字段吗?您所说的
高文章计数是什么意思?
?您好@JPG,您知道了--我在问题中将日期字段定义添加到了模型中。“high_article_count”是一个数字,但为了清楚起见,我刚刚更新了我的问题,使其包含数字,而不是更奇怪的变量名。
from django.db.models import Count

Website.objects.annotate(articlesite_count=Count('articlesite')).filter(articlesite_count__gte=100)
from django.db.models import Count

Website.objects.filter(articlesite__edited_date__isnull=True).annotate(
    articlesite_count=Count('articlesite')).filter(
    articlesite_count__gte=100)