1) зачем нужен `select_related("gender", "categories")` ? ведь и то и другое не являются relations
2) всегда ли gender="man" ?
в итоге я бы сделал так
GENDER_CATEGORIES = [
("men", 1),
("men", 2),
("men", 3)
]
from django.db.models import Q
def catalog(request):
filters = [Q(gender=gender, categories=category_id) for gender, category_id in GENDER_CATEGORIES]
query = Q()
for q_filter in filters:
query |= q_filter
products = Products.objects.filter(query).only("id", "title", "price", "image")
products_by_category = {category[1]: [] for category in GENDER_CATEGORIES}
for product in products:
for gender, category_id in GENDER_CATEGORIES:
if product.gender == gender and product.categories == category_id:
products_by_category[category_id].append(product)
break
context = {"products_by_category": products_by_category}
return render(request, 'catalog.html', context)
ну и еще в тапл добавить имя 3-м параметром