2013-07-09 2 views
0

이 방법이 효과적이지만 더 나은 장고 관용어가 있습니까?저자의 발행자 목록

my_books = Book.objects.filter(author__name=='me') 
my_publishers = Publisher.objects.filter(pk__in=[b.publisher.id for b in my_books]) 

models = round_up_the_usual_suspects() 

class Publisher(models.Model): 
    name = models.CharField(max_length=30) 


class Author(models.Model): 
    name = models.CharField(max_length=30) 


class Book(models.Model): 
    title = models.CharField(max_length=100) 
    author = models.ForeignKey(Author, related_name='books_authored') 
    publisher = models.ForeignKey(Publisher, related_name='books_published') 
+0

에 의해 작성된 책에 대한 모든 출판사를 제공

my_publishers = Publisher.objects.filter(book__author__name='me') 

할 수 있습니다. 따라서 @karthikrs 제안은 가장 의미가 있습니다. –

답변

1

당신이 당신에게 일반적으로 당신이와 끝까지하려는 관계를 시작하는 것입니다 me

+0

간단하지만 (내 경우에는) 직관적이지 않습니다. 고맙습니다 –