搜索
REST framework有提供了一个比较简洁的搜索功能给我们使用。
class GoodsListViewSet(viewsets.ModelViewSet):
#数据
queryset = Goods.objects.all()
# 指定序列化的类
serializer_class = GoodsSerializer
#配置搜索功能
filter_backends = (filters.SearchFilter,)
#设置搜索的关键字
search_fields=('name','goods_brief','goods_desc')
'''
^ :搜索关键字开头的数据
= :完全匹配搜索 =name :则是完全匹配,不能模糊搜索
@ :全文搜索(目前只支持MySQL)
$ :正则表达式搜索
'''
在没有配置filter_backends之前,显示的效果如下:
配置filter_backends之后:会出现一个过滤器
class GoodsListViewSet(viewsets.ModelViewSet):
serializer_class=GoodsSerializer
#自定义过滤
def get_query(self):
price_min=self.request.query_params.get('price_min',0)
if price_min:
queryset=queryset.filter(shop_price__gt=int(price_min))
return queryset
首先安装django-filter:pip install django-filter , 然后注册到app
自定义过滤类:
class GoodsFilter(django_filters.rest_framework.FilterSet):
"""
商品的过滤类
"""
pricemin = django_filters.NumberFilter(field_name='shop_price', help_text="最低价格", lookup_expr='gte')
pricemax = django_filters.NumberFilter(field_name='shop_price', lookup_expr='lte')
top_category = django_filters.NumberFilter(method='top_category_filter')
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', 'is_hot', 'is_new']
views.py:
class GoodsListViewSet(viewsets.ModelViewSet):
# 数据
queryset = Goods.objects.all()
# 指定序列化的类
serializer_class = GoodsSerializer
# 配置搜索功能
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
# 设置搜索的关键字
# filter_fields = ('name',) # 默认是全匹配,可自定义
filter_class = GoodsFilter # 自定义过滤
search_fields = ('name', 'goods_brief', 'goods_desc') # 搜索
效果如下:
1、全局分页
在settings.py文件中设置:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination'
}
常见参数:
2、自定义分页
# 定制分页
class GoodsPagination(PageNumberPagination):
page_size = 12 # 每页显示多少个
page_size_query_param = 'page_size'
page_query_param = 'page' # 查询参数的名称。默认是’page‘
max_page_size = 100
views.py:
class GoodsListViewSet(viewsets.ModelViewSet):
# 数据
queryset = Goods.objects.all()
# 指定序列化的类
serializer_class = GoodsSerializer
# 配置搜索功能
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
# 设置搜索的关键字
# filter_fields = ('name',) # 默认是全匹配,可自定义
filter_class = GoodsFilter # 自定义过滤
search_fields = ('name', 'goods_brief', 'goods_desc')
pagination_class = GoodsPagination # 自定义分页设置
在views.pywe的filter_backends添加 filters.OrderingFilter
class GoodsListViewSet(viewsets.ModelViewSet):
# 数据
queryset = Goods.objects.all()
# 指定序列化的类
serializer_class = GoodsSerializer
# 配置搜索功能
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
# 设置搜索的关键字
# filter_fields = ('name',) # 默认是全匹配,可自定义
filter_class = GoodsFilter # 自定义过滤
search_fields = ('name', 'goods_brief', 'goods_desc')
pagination_class = GoodsPagination # 自定义分页设置
#添加排序字段
ordering_fields = ('sold_num', 'shop_price')
效果如下: