2

I am working with Django 1.6 and have a problem statement as follows:

  1. Suppose there are 2 models, Author and Book, so there is obvious one-to-many relation for the author and book model.
  2. Now, I need to display the Author's data such that, there is one row for each Book he has written.
  3. As the UI part is in backbone js, its hard for me to iterate/display by modifying things in js.
  4. So, would like to have this being done at the backend itself. Data to be displayed like:

    Authors name|Authors Qualification|Authors age|Book|Price
    

So, I have a Queryset which has , and within this, if i fetch the Books he has published, it gives me all the books published by him. Instead i want them one by one.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Varun J
  • 21
  • 1
  • 1
    it is not clear to me what it is you are asking. What do you mean *it gives me all the books published by him. Instead i want them one by one* – Nikos M. Dec 20 '19 at 10:55

1 Answers1

2

Just focus on the Books and use .select_related(..) to fetch the Authors as well. For example we can print something like:

books = Book.objects.select_related('author').order_by('author__name')
for book in books:
    print(book.author.name, book.author.qualification, book.author.age, book.title, book.price)

I am working with Django 1.6.

is no longer supported since April 1, 2015. Please try to upgrade as soon as possible.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555