views:

8

answers:

1

Please forgive me if this question is ridiculous. I'm relatively new to programming and Django.

Following the typical Publisher, Author, Book example....

Publisher to Author (One to Many) Author to Book (One to Many)

I'm trying to get Django to give me a list of books published by all the authors under a single publisher.


Two ways I've thought about doing this:

  1. I could get a list of all the authors under a single publisher, and then iterate over these authors to find all of their books. This seems like a really wonky way to do this.. and I'll have to hit the DB a lot.

  2. I can get Django to add a new "Publisher" column in my Books table, but I'm not sure if I want to replicate that data, and I don't know how to get Django to automatically fill in that info.

I'd appreciate any help. Thanks.

A: 

If each book has an author ForeignKey and each author has a publisher ForeignKey, you can do:

Book.objects.filter(author__publisher = <Publisher object>)

To get what you're looking for.

adamk
Ahhhhh. Worked like a charm. Reading the docs on queries now.Thank you!
Rob