How to filter product by Category wise in Django?

无人久伴 提交于 2020-08-10 19:12:30

问题


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.

here is my models.py file...

 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)

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.


回答1:


You can try 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)

EDIT: 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()


来源:https://stackoverflow.com/questions/63046835/how-to-filter-product-by-category-wise-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!