如何在 Django 中按类别筛选产品?
Posted
技术标签:
【中文标题】如何在 Django 中按类别筛选产品?【英文标题】:How to filter product by Category wise in Django? 【发布时间】:2020-11-12 17:47:46 【问题描述】:我正在尝试根据类别进行过滤,但它在每个类别页面上显示所有产品,但我想根据类别页面进行过滤,请检查我的代码并告诉我该怎么做。
这是我的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
进行过滤时,所有产品都会显示在每个类别页面上.请合并这两个代码并给我一个正确的解决方案。
【问题讨论】:
【参考方案1】:例如:
在models.py
from django.db import models
class Category(model.Model):
slug = models.SlugField(unique=True, max_length=50)
class Item(model.Model):
category = models.ManyToManyField(Category,related_name='ITEM')
在views.py
from django.shortcuts import render, get_object_or_404
from .models import Category
def category_filter(request, slug):
category = get_object_or_404(Category, slug=slug)
data = category.ITEM.all()
render(request, TEMPLATE_NAME, context='data':data)
在 urls.py 中
from django.urls import path
from .views import category_filter
urlpatterns = [
path('category/<slug:slug>/', category_filter, name='Category'),
]
现在如果 url 类似于
本地主机/类别/froots/
将显示所有具有 froot 类别的项目。
【讨论】:
注意:如果你使用通用视图,你应该从self.kwargs.get('slug')
加载slug【参考方案2】:
你可以试试这个。
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)
编辑:这里没有必要编写额外的filter
方法。
您需要的基本内容是您需要首先获取单个类别对象并从产品模型中过滤该类别的产品。
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)
您也可以像这样过滤相关产品(其他方式)
subcategory = get_object_or_404(SubCategory, subcat_slug=subcat_slug)
productlist = subcategory.prosubcat.all()
【讨论】:
使用filter
我在每个类别页面(https://prnt.sc/tn0sah
)上获取所有产品,并使用product
我根据特定类别获取产品,但我想要每个类别中的产品应用过滤器后。那么我如何使用这个f = ProductFilter(request.GET, queryset=Product.objects.all()
获得subcategory
产品,任何其他解决方案都值得赞赏
@sainitor 你不需要写额外的filter
方法。您可以简单地从 Product 模型中获取类别对象和过滤产品
你有这方面的参考吗,我可以使用Product
模型过滤产品...
您可以查看here了解更多详情以上是关于如何在 Django 中按类别筛选产品?的主要内容,如果未能解决你的问题,请参考以下文章