from django.db.models import Q
from collections import defaultdict
def catalog(request):
categories = {
1: "wallets",
2: "bags",
3: "belts",
...
}
products = Products.objects.filter(gender="men").filter(categories__in=categories.keys()). select_related("gender", "categories").only("id", "title", "price", "image")
categorized_products = defaultdict(list)
for product in products:
category = categories[product.categories_id]
categorized_products[category].append(product)
context = categorized_products
return render(request, 'catalog.html', context)
module = importlib.import_module(f'actions.{module_name}')
class category(models.Model):
category_name = models.CharField(max_length=70, verbose_name='Категория')
class Meta:
verbose_name = "Категории товаров"
def __str__(self):
return self.category_name
def get_related_items(self):
related_items = []
related_items.extend(list(self.sup_set.all()))
related_items.extend(list(self.paddle_set.all()))
related_items.extend(list(self.boot_set.all()))
return related_items
{% for category in categories %}
<h2>{{ category.category_name }}</h2>
<ul>
{% for item in category.get_related_items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
{% endfor %}