现在情况是这样 我们现在返回给前端的商品是所有商品, 但是前端先现在需要得到一级分类以下所有的商品。
咋搞:
filter.py
import django_filters
from django.db.models import Q
from .models import Goods
class GoodsFilter(django_filters.rest_framework.FilterSet):
"""
商品的过滤类
"""
pricemin = django_filters.NumberFilter(field_name='shop_price', lookup_expr='gte')
pricemax = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte')
# 获取相应类别下的所有商品
top_category = django_filters.NumberFilter(method='top_category_filter')
# 这是全部匹配
name = django_filters.CharFilter(field_name='name')
# 该函数返回前端请求的商品类别等级相符的所有商品
def top_category_filter(self, queryset, name, 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 = ['pricemin','pricemax', 'name']
嗯。。6.4 6.5主要说的就是这货 其他都是讲vue前端如何调用如何传递数据 嗯。。。。vue的知识我回头自己摸 这个教程最重要的是搞会rest_framework怎么用