这有效,但有没有更好的Django成语?
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 :(得分:1)
你可以做到
my_publishers = Publisher.objects.filter(book__author__name='me')
这为您提供了由me