6-5 vue展示商品列表页数据
C:\Users\huang\PycharmProjects\MxShop\apps\goods\filters.pyimport django_filtersfrom django.db.models import Qfrom .models import Goodsclass GoodsFilter(django_filters.rest_framework.Fil...
·
C:\Users\huang\PycharmProjects\MxShop\apps\goods\filters.py
import django_filters
from django.db.models import Q
from .models import Goods
class GoodsFilter(django_filters.rest_framework.FilterSet):
"""
商品 过滤类
"""
price_min = django_filters.NumberFilter(field_name='shop_price', lookup_expr='gte')
price_max = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte')
# contains 是模糊查询 加上 i 不区分大小写
name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
top_category = django_filters.NumberFilter(method='top_category_filter')
def top_category_filter(self, queryset, name, value):
# queryset = queryset.filter(Q(category_id=value)|Q(category__parent_category_id=value)|Q(category__parent_category__parent_category_id=value))
return queryset.filter(Q(category_id=value)|Q(category__parent_category_id=value)|Q(category__parent_category__parent_category_id=value))
class Meta:
model = Goods
fields = ['price_min', 'price_max', 'name']
下面测试一下接口:
比如搜索id 为1的
结果有14个数据:
------------
查询结果总数:
this.proNum = response.data.count;
排序:
# ordering_fields = ('sold_num', 'add_time')
ordering_fields = ('sold_num', 'shop_price')
-------
=====
显示有问题
更多推荐
已为社区贡献1条内容
所有评论(0)