Попытка получить детей от отношения один ко многим, используя свойство related_name.
То, что я пробовал до сих пор, не работает:
models.py
class Category(models.Model):
name = models.CharField(max_length=30, unique=True)
slug = models.SlugField(max_length=30, unique=True)
class Product(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=30, unique=True)
category = models.ForeignKey(
Category,
related_name='products',
on_delete=models.PROTECT,
default=1
)
views.py
from django.shortcuts import render, get_object_or_404
from Shop.models import Category
def product_list(request, slug):
category = get_object_or_404(Category, slug=slug)
products = category.products.all()
context = {
'customCSS': '/static/css/product_list.min.css',
'title': category.name,
}
return render(request, template, context)
product_list.html
{% block content %}
<ul>
{% for product in products %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
{% endblock %}