作者的出版商名单

时间:2013-07-09 17:09:48

标签: django django-models django-related-manager

这有效,但有没有更好的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')

1 个答案:

答案 0 :(得分:1)

你可以做到

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

这为您提供了由me

撰写的图书的所有发布商