django从查询集中获取不同的外键
我有一个很大的 django MySQL 数据库,我正在努力让它高效工作:
models.py :
class Category(models.Model)
name = models.CharField()
class Article(models.Model)
start_date = models.DateTimeField(...)
end_date = models.DateTimeField(...)
active = models.BooleanField(...)
categories = models.ManyToManyField( Category )
我想根据文章查询集获取所有活动类别。我实际上是这样做的:
actives_articles = Articles.objects.filter(start_date__gt = datetime.datetime.today(), end_date__lt = another_date, active = True)
actives_categories = Category.objects.filter(article__in = actives_articles).distinct().order_by('name')
actives_articles
返回大约 50k 结果,所以这根本没有效率。
有什么想法或指示吗?
谢谢 !
I have a big django MySQL database and im struggling to get this to work efficiently :
models.py :
class Category(models.Model)
name = models.CharField()
class Article(models.Model)
start_date = models.DateTimeField(...)
end_date = models.DateTimeField(...)
active = models.BooleanField(...)
categories = models.ManyToManyField( Category )
I'd like to get all the active categories based on Article queryset. I actually do it this way :
actives_articles = Articles.objects.filter(start_date__gt = datetime.datetime.today(), end_date__lt = another_date, active = True)
actives_categories = Category.objects.filter(article__in = actives_articles).distinct().order_by('name')
actives_articles
return about 50k results so this is not efficient at all.
Any idea or pointers ?
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于得到了一些与此相关的东西:
愚蠢的我,谢谢 Django!
I finally got something working with this :
silly me and thanks Django !