rest_framework的filter

setting中添加

INSTALLED_APPS = [
    'django_filters',
]

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
} 

views中添加

from django_filters.rest_framework import DjangoFilterBackend

class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    List all goods.
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer
    fileter_backends = (DjangoFilterBackend)
    filter_fields = ('name', 'shop_price')

遇到的问题

  1、页面上没有出现过滤器

  原因是setting中没配置REST_FRAMEWORK,配置好再刷新页面就有过滤器按钮了

效果展示

rest_framework的filter

 

上一篇:关于response.sendError()方法的参数


下一篇:8.过滤器Filter