简体   繁体   English

Django多对多查询集过滤器

[英]Django Many to Many Query Set Filter

I'm able to query my database below to get the result that I want but I don't want to have to iterate through all of the author objects, just the ones that have more than one post. 我可以在下面查询我的数据库以获得所需的结果,但是我不想遍历所有author对象,而仅遍历具有多个帖子的对象。 There is a many-to-many relationship between Authors and Posts with the Many-To-ManyField on the Post. 作者和帖子之间存在多对多关系,帖子上具有“多对多”字段。 Does anyone know how to make this more efficient? 有谁知道如何提高效率?

for author in Author.objects.all():
    if len(author.post_set.all()) > 0:
        print author

Use this: 用这个:

for author in Author.objects.filter(post__isnull=False):
    print author

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM