[Answer]-Retrieve data from two tables with foreign key relationship in Django?

1πŸ‘

βœ…

The Following views and html is used for display all books with corrosponding author details.

views.py

def client_add(request):
   books = Book.objects.all()
   return render_to_response('book_details.html', locals(),    context_instance=RequestContext(request))

book_details.html

<body>
{% for book in books %}
{{book.book_name}}
{{book.publisher_name}}
{{book.author.first_name}}
{{book.author.last_name}}
{{book.author.email}}
{{book.author.age}}
{% endif %}
</body>

The Following views and html is used for display books for particular author corrosponding details.

views.py

def client_add(request):
   books = Book.objects.all(author_last_name ="author_last_name")
   return render_to_response('book_details.html', locals(),    context_instance=RequestContext(request))

book_details.html

<body>
{% for book in books %}
{{book.book_name}}
{{book.publisher_name}}
{{book.author.first_name}}
{{book.author.last_name}}
{{book.author.email}}
{{book.author.age}}
{% endif %}
</body>
πŸ‘€gangakvp

Leave a comment