如何从特定主要类别的所有子类别中获取所有项目? 例如,我的主要类别为“衣服”,它的子类别为“毛衣”,“ T恤衫”等,而它们的子类别为“ gucci”,“ armani”等。我试图编写一个函数,但只显示了一个功能。 1级分类中的元素。例如,如果我要搜索衣服,它应该显示与此类别有关的所有产品
这是我的产品。浏览量
from .models import Product
from .serializers import ProductSerializer
from rest_framework import generics
from django.db.models import Q
class ProductsList(generics.ListCreateAPIView):
queryset = Product.instock.all().order_by('-id')
serializer_class = ProductSerializer
def get_queryset(self):
queryset = Product.instock.all()
title = self.request.query_params.get('title', None)
color = self.request.query_params.get('color', None)
price = self.request.query_params.get('price', None)
category = self.request.query_params.get('category', None)
if category is not None:
queryset = queryset.filter(Q(category__parent_category__title__icontains=category) |
Q(category__title__icontains=category))
if title is not None:
queryset = queryset.filter(title__icontains=title)
if color is not None:
queryset = queryset.filter(color__icontains=color)
if price is not None:
queryset = queryset.filter(price__iexact=price)
return queryset