zoukankan      html  css  js  c++  java
  • django drf Filter

    1.定义get_queryset()方法

    from django.shortcuts import render
    
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import status
    from rest_framework.parsers import JSONParser
    from rest_framework import mixins, generics
    from rest_framework.generics import ListAPIView
    from rest_framework.pagination import PageNumberPagination
    from goods.models import Goods, GoodsCategory
    from goods.serializer import GoodsSerializer, CategorySerializer
    from rest_framework import viewsets
    from django_filters.rest_framework import DjangoFilterBackend
    
    class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet):
        class GoodsPagination(PageNumberPagination):
            page_size = 2
            page_size_query_param = 'pageSize'
            page_query_param = 'p'
            max_page_size = 100
    
        # queryset = Goods.objects.all()  # 不能切片后再过滤,例如:Goods.objects.all()[:10]
        serializer_class = GoodsSerializer
        pagination_class = GoodsPagination
    
    
        # 过滤方法1
        # 使用此方法,不使用queryset时,须在urls.router中指定base_name参数
        def get_queryset(self):
            queryset = Goods.objects.all() #此时并没有取出数据
            price_min = self.request.query_params.get('price_min',0)
            if price_min:
                queryset = queryset.filter(shop_price__gt=int(price_min))
            return queryset

    须在urls中指定base_name

    router = DefaultRouter()
    router.register('goods',GoodsList,base_name='a')

    test

    2.django-filter

    安装步骤相见文档https://django-filter.readthedocs.io/en/master/guide/install.html

    https://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend

    View中代码

    from django.shortcuts import render
    
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import status
    from rest_framework.parsers import JSONParser
    from rest_framework import mixins, generics
    from rest_framework.generics import ListAPIView
    from rest_framework.pagination import PageNumberPagination
    from goods.models import Goods, GoodsCategory
    from goods.serializer import GoodsSerializer, CategorySerializer
    from rest_framework import viewsets
    from django_filters.rest_framework import DjangoFilterBackend
    
    class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet):
        class GoodsPagination(PageNumberPagination):
            page_size = 2
            page_size_query_param = 'pageSize'
            page_query_param = 'p'
            max_page_size = 100
    
        queryset = Goods.objects.all()  # 不能切片后再过滤,例如:Goods.objects.all()[:10]
        serializer_class = GoodsSerializer
        pagination_class = GoodsPagination
        filter_backends = (DjangoFilterBackend,)
        filter_fields = ('name',) #逗号必加,缺点无法模糊查询

    test

    3.自定义filter

    from django.shortcuts import render
    
    from rest_framework.views import APIView
    from rest_framework.response import Response
    from rest_framework import status
    from rest_framework.parsers import JSONParser
    from rest_framework import mixins, generics
    from rest_framework.generics import ListAPIView
    from rest_framework.pagination import PageNumberPagination
    from goods.models import Goods, GoodsCategory
    from goods.serializer import GoodsSerializer, CategorySerializer
    from rest_framework import viewsets
    from django_filters.rest_framework import DjangoFilterBackend
    
    import django_filters
    class GoodsFilter(django_filters.rest_framework.FilterSet):
        max_price = django_filters.rest_framework.NumberFilter(field_name='shop_price',lookup_expr='lte')
        min_price = django_filters.rest_framework.NumberFilter(field_name='shop_price',lookup_expr='gte')
    
        class Meta:
            model = Goods
            fields  = ['min_price','max_price']
    
    
    class GoodsList(mixins.ListModelMixin, viewsets.GenericViewSet):
        class GoodsPagination(PageNumberPagination):
            page_size = 2
            page_size_query_param = 'pageSize'
            page_query_param = 'p'
            max_page_size = 100
    
        queryset = Goods.objects.all()  # 不能切片后再过滤,例如:Goods.objects.all()[:10]
        serializer_class = GoodsSerializer
        pagination_class = GoodsPagination
        filter_backends = (DjangoFilterBackend,)
        # filter_fields = ('name',) #逗号必加,缺点无法模糊查询
        filterset_class  = GoodsFilter

    test

  • 相关阅读:
    C++学习笔记十关联容器
    Ubuntu下使用GDB断点Go程序
    各种语言的数字转罗码方法的实现
    为什么 ++i和i++的效果是一样的,试了javascript ,c++ java
    罗马数字转换阿拉伯数字(Java版,考虑较为全面)
    C++学习笔记九顺序容器(二) ForFreeDom 博客园
    智立方 屁话真言108:能盛事者能成事_智立方的杨石头_新浪博客
    腾讯搜搜高管吴军离职的传闻与真相
    罗马数字_百度百科
    快速深入一门语言的几个问题 Shell909090 随笔杂记
  • 原文地址:https://www.cnblogs.com/chenyishi/p/10656604.html
Copyright © 2011-2022 走看看