【问题标题】:Django Many to Many Query Set FilterDjango 多对多查询集过滤器
【发布时间】:2015-01-12 17:45:39
【问题描述】:

我可以在下面查询我的数据库以获得我想要的结果,但我不想遍历所有作者对象,只是那些有多个帖子的对象。作者和帖子之间存在多对多关系,帖子上的多对多字段。有谁知道如何提高效率?

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

【问题讨论】:

    标签: python django django-models many-to-many


    【解决方案1】:

    使用这个:

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

    【讨论】:

      猜你喜欢
      • 2015-02-17
      • 1970-01-01
      • 2013-08-18
      • 2011-11-07
      • 2020-04-30
      • 2018-12-01
      • 2019-09-20
      • 2019-02-09
      • 2020-01-24
      相关资源
      最近更新 更多