我正在使用 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.
1 回答
翻阅古今
TA贡献1780条经验 获得超5个赞
from django.db.models import Count
Website.objects.annotate(articlesite_count=Count('articlesite')).filter(articlesite_count__gte=100)
更新-1
from django.db.models import Count
Website.objects.filter(articlesite__edited_date__isnull=True).annotate(
articlesite_count=Count('articlesite')).filter(
articlesite_count__gte=100)
添加回答
举报
0/150
提交
取消