rest_framework使用过滤器django-filters

rest_framework使用过滤器django-filters

普通过滤

  • view

    from rest_framework.pagination import PageNumberPagination
    from rest_framework import mixins
    from rest_framework import viewsets
    from django_filters.rest_framework import DjangoFilterBackend
    
    
    from .models import Goods
    from .serializers import GoodsSerializer
    
    
    class GoodsPagination(PageNumberPagination):
        page_size = 10
        page_size_query_param = "page_size"
        page_query_param = "p"
        max_page_size = 100
    
    
    class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
        # 指定queryset
        queryset = Goods.objects.all()
    
        # 指定序列化类
        serializer_class = GoodsSerializer
    
        # 指定分页类
        pagination_class = GoodsPagination
    
        # 添加过滤器
        filter_backends = [DjangoFilterBackend]
    
        # 只需要简单的基于等同的过滤,则可以filter_fields在视图或视图集上设置属性,列出要过滤的字段集。
        # 等同就是根据你过滤的字段的数据必须跟数据库里那个字段的数据相同
        filter_fields = ['name', 'shop_price']
    
  • settings

    INSTALLED_APPS = [
        # drf过滤器 也 是xadmin的过滤器
        'django_filters',
        'rest_framework',
    ]
    
  • url

    from django.conf.urls import url, include
    
    from rest_framework.routers import DefaultRouter
    
    from goods.views import GoodsListViewSet
    # 生成一个注册器实例对象
    router = DefaultRouter()
    # 将需要自动生成url的接口注册
    # 配置goods的url
    router.register(r'goods', GoodsListViewSet, base_name="goods-list")
    
    urlpatterns = [
        # 自动生成url
        url(r"^", include(router.urls)),
    ]
    
  • 效果图
    rest_framework使用过滤器django-filters_第1张图片
    rest_framework使用过滤器django-filters_第2张图片
    rest_framework使用过滤器django-filters_第3张图片

高级过滤

  • view

    from rest_framework.pagination import PageNumberPagination
    from rest_framework import mixins
    from rest_framework import viewsets
    from django_filters.rest_framework import DjangoFilterBackend
    
    
    from .models import Goods
    from .serializers import GoodsSerializer
    # 导入过滤器类
    from .filters import GoodsFilter
    
    
    class GoodsPagination(PageNumberPagination):
        page_size = 10
        page_size_query_param = "page_size"
        page_query_param = "p"
        max_page_size = 100
    
    
    class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
        # 指定queryset
        queryset = Goods.objects.all()
    
        # 指定序列化类
        serializer_class = GoodsSerializer
    
        # 指定分页类
        pagination_class = GoodsPagination
    
        # 添加过滤器
        filter_backends = [DjangoFilterBackend]
    
        # 指定过滤器类
        filter_class = GoodsFilter
    
  • 过滤器类—根据你的需求自己定义

    import django_filters
    
    from .models import Goods
    
    
    class GoodsFilter(django_filters.rest_framework.FilterSet):
        """
        商品的过滤类
        """
    
        # 大于这个值 name指定字段  lookup_expr过滤条件
        # NumberFilter 数字类型
        price_min = django_filters.NumberFilter(name="shop_price", lookup_expr="gte")
        # 小于这个值
        price_max = django_filters.NumberFilter(name="shop_price", lookup_expr="lte")
    
        # name模糊查询, 不指定过滤条件, 必须全部匹配
        # CharFilter字符串类型, 详情看官方文档
        name = django_filters.CharFilter(name="name", lookup_expr="icontains")
    
        class Meta:
            # 指定模型类
            model = Goods
            # 显示这两个字段
            fields = ["price_min", "price_max", "name"]
    
    
  • settings

    INSTALLED_APPS = [
        # drf过滤器 也 是xadmin的过滤器
        'django_filters',
        'rest_framework',
    ]
    
  • url

    
    
    from django.conf.urls import url, include
    
    from rest_framework.routers import DefaultRouter
    
    from goods.views import GoodsListViewSet
    # 生成一个注册器实例对象
    router = DefaultRouter()
    # 将需要自动生成url的接口注册
    # 配置goods的url
    router.register(r'goods', GoodsListViewSet, base_name="goods-list")
    
    urlpatterns = [
        # 自动生成url
        url(r"^", include(router.urls)),
    ]
    
  • 效果图
    rest_framework使用过滤器django-filters_第4张图片

你可能感兴趣的:(#,rest_framework,过滤器)