Reputation: 371
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
def __unicode__(self):
return self.title
i want to get all Publishers who has published at least one book.
Upvotes: 0
Views: 32
Reputation: 48952
Publisher.objects.filter(book__isnull=False).distinct()
This does a JOIN
between the two tables and returns the rows where a book exists. distinct()
is used to get rid of duplicate Publishers
.
Upvotes: 1