0

这行得通,但是有没有更好的 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')
4

1 回答 1

1

你可以做

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

这为您提供了由me

于 2013-07-09T17:17:41.963 回答