ManyToMany过滤器匹配列表中的所有项目

ManyToMany过滤器匹配列表中的所有项目

本文介绍了Django:ManyToMany过滤器匹配列表中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一本书模型:

class Book(models.Model):
    authors = models.ManyToManyField(Author, ...)
    ...

简而言之:

我想检索作者严格等同于一组给定作者的图书。我不知道是否有一个单一的查询,但是任何建议都将是有帮助的。

I'd like to retrieve the books whose authors are strictly equal to a given set of authors. I'm not sure if there is a single query that does it, but any suggestions will be helpful.

长:

这是我尝试的(没有运行得到AttributeError)

Here is what I tried, (that failed to run getting an AttributeError)

# A sample set of authors
target_authors = set((author_1, author_2))

# To reduce the search space,
# first retrieve those books with just 2 authors.
candidate_books = Book.objects.annotate(c=Count('authors')).filter(c=len(target_authors))

final_books = QuerySet()
for author in target_authors:
    temp_books = candidate_books.filter(authors__in=[author])
    final_books = final_books and temp_books

...这里是我所得到的:

... and here is what I got:

AttributeError: 'NoneType' object has no attribute '_meta'

一般来说,我应该如何查询一个模型,其约束是它的ManyToMany字段包含一组给定的对象在我的情况下?

In general, how should I query a model with the constraint that its ManyToMany field contains a set of given objects as in my case?

ps:我发现一些相关的SO问题,但无法得到一个明确的答案。任何好的指针也将有所帮助。谢谢。

ps: I found some relevant SO questions but couldn't get a clear answer. Any good pointer will be helpful as well. Thanks.

推荐答案

类似于@ goliney的方法,我找到了一个解决方案。但是,我认为效率可以提高。

Similar to @goliney's approach, I found a solution. However, I think the efficiency could be improved.

# A sample set of authors
target_authors = set((author_1, author_2))

# To reduce the search space, first retrieve those books with just 2 authors.
candidate_books = Book.objects.annotate(c=Count('authors')).filter(c=len(target_authors))

# In each iteration, we filter out those books which don't contain one of the
# required authors - the instance on the iteration.
for author in target_authors:
    candidate_books = candidate_books.filter(authors=author)

final_books = candidate_books

这篇关于Django:ManyToMany过滤器匹配列表中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:49