Answer the question
In order to leave comments, you need to log in
How to display a list of categories and related products on one page?
There are two categories:
class Category(models.Model):
name = models.CharField(_('Название категории'), max_length=64)
...
class Book(models.Model):
title = models.CharField(_('Название книги'), max_length=256)
category = models.ForeignKey(Category, related_name='category_book')
...
Answer the question
In order to leave comments, you need to log in
{% for category in categorys %}
{{ category }}
{% for book in category.category_book.all %}
{{ book }}
{% endfor %}
{% endfor %}
This is in the template, but in the view:
model = Category
context_object_name = 'categories'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question