如何在Django中按类别明智地过滤产品? [英] How to filter product by Category wise in Django?
问题描述
我正在尝试根据类别进行过滤,但它会在每个类别页面上显示所有产品,但是我想根据类别页面进行过滤,请检查我的代码并告诉我该怎么做。
I am trying to filter according to the category but it's displaying all products on each category page, but I want filter according to the category page, please check my code and let me know how I can do it.
这是我的 models.py
文件...
class SubCategory(models.Model):
subcat_name=models.CharField(max_length=225)
subcat_slug=models.SlugField(max_length=225, unique=True)
category = models.ForeignKey('Category', related_name='subcategoryies', on_delete=models.CASCADE, blank=True, null=True)
and here is my product models.py file...
class Product(models.Model):
name=models.CharField(max_length=225)
slug=models.SlugField(max_length=225, unique=True)
subcategory=models.ForeignKey('SubCategory', related_name='prosubcat', on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.name
class ProductFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Product
fields = ['saleprice', 'title','veg_non','brand','supplement']
def SubCategorySlugListAPIView(request, subcat_slug):
category = Category.objects.all()
subcategories = SubCategory.objects.all()
product = Product.objects.all()
brands=Brand.objects.all()
f = ProductFilter(request.GET, queryset=Product.objects.all())
supplement=Supplement.objects.all()
featured=Product.objects.filter(featured=True).order_by('-created_at')[0:6]
high = Product.objects.all().order_by('-saleprice')
if subcat_slug:
subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)
productlist = product.filter(subcategory=subcategory)
paginator = Paginator(productlist, 12)
page_number = request.GET.get('page')
product = paginator.get_page(page_number)
template_name = 'mainpage/cat-products.html'
context = {'product': product,
'subcategories': subcategories, 'subcategory': subcategory, 'category': category, 'featured':featured, 'high':high, 'brands':brands, 'supplement':supplement, 'filter':f}
return render(request, template_name, context)
我知道有必要合并这些代码 f = ProductFilter(request.GET,queryset = Product.objects.all())
和此 productlist = product.filter(subcategory = subcategory)
,使用 productlist
我正在根据类别获取产品,但是当我使用 filter
然后在每个类别页面上显示所有产品。请合并两个代码,并为此提供正确的解决方案。
i know there are need to merge these code f = ProductFilter(request.GET, queryset=Product.objects.all())
and this productlist = product.filter(subcategory=subcategory)
, using productlist
i am getting product according to the category but when I do filter with filter
then all products displaying on each category page. please merge both code and give me a correct solution for this.
推荐答案
您可以尝试一下。
def SubCategorySlugListAPIView(request, subcat_slug):
# First get the category object.
subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)
# then filter the products on this category
productlist =Product.objects.filter(subcategory=subcategory)
编辑:无需编写额外的过滤器
方法。
基本需求是您需要首先获取单个类别对象,然后从产品模型中过滤该类别的产品。
There is no necessary to write extra filter
method here.
The basic thing you need is you need to get the single category object first and filter the products on this category from Product model.
def SubCategorySlugListAPIView(request, subcat_slug):
subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)
productlist = Product.objects.filter(subcategory=subcategory)
paginator = Paginator(productlist, 12)
page_number = request.GET.get('page')
product = paginator.get_page(page_number)
brands=Brand.objects.all()
supplement=Supplement.objects.all()
featured=Product.objects.filter(featured=True).order_by('-created_at')[0:6]
high = Product.objects.all().order_by('-saleprice')
template_name = 'mainpage/cat-products.html'
context = {'product': product,
'subcategories': subcategories, 'subcategory': subcategory, 'category': category, 'featured':featured, 'high':high, 'brands':brands, 'supplement':supplement}
return render(request, template_name, context)
也您可以像这样过滤相关产品(其他方式)
Also you can filter the related products like this(other way)
subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)
productlist = subcategory.prosubcat.all()
这篇关于如何在Django中按类别明智地过滤产品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!