2017-12-26 20 views
0

モデルで外部キーを逆に?Djangoは

たとえば、( 'A'、 'B'、 'C​​')の書籍(およびそれ以上のもの)、つまりサブセットを含むすべての人物を選択します。

答えて

1

intersectionを使用してみてください:

queryset = Person.objects.filter(books__title='A').intersection(Person.objects.filter(books__title='B'), Person.objects.filter(books__title='C')) 

を使用している場合Djangoは1.11より古い:

queryset = Person.objects.filter(books__title='A') & Person.objects.filter(books__title='B') & Person.objects.filter(books__title='C') 
+0

私は完全一致を望む場合はどうすればいいですか? –

+0

私は自分の答えを更新しました。 – Ivan

1

あなたの本の特定のセットの作者のセットを取得する場合:

Person.object.filter(books__in=Book.objects.filter(name__in=['A','B','C'])) 

または短い:

Person.object.filter(books__name__in=['A','B','C'])) 
+0

私は完全な平等を望みますか? –

+0

@AndrewFount正確な平等とは何ですか? –

+0

正確に['A'、 'B'、C '] –