zoukankan      html  css  js  c++  java
  • Django:drf过滤、搜索、排序功能

    过滤功能实现

    1.get_query_set方法过滤

    编辑blogs目录下的views.py,新增get_queryset方法

    class ArticleListViewset(mixins.ListModelMixin,
                             mixins.CreateModelMixin,
                             viewsets.GenericViewSet):
        serializer_class = ArticleSerializer
        pagination_class = StandardResultsSetPagination
        def get_queryset(self):
            queryset = Article.objects.all()
            click_num = self.request.query_params.get('click', 0)
            if click_num:
                queryset = queryset.filter(click_num__gte=30)
            return queryset

    在浏览器输入 http://localhost:8000/api/v1/articles/?click=50 ,click代表参数值过滤了大于50点击数的文章。但是当过滤参数多的时候此过滤方法写起来比较麻烦

    2.django-filters方法过滤

    pip install django-filter 安装django-filter

    在settings.py注册配置django-filters,在REST_FRAMEWORK 加上过滤器会对全局生效,如果只针对特定视图可以单独在view里面加

    ①settings.py全局过滤器

    编辑settings.py

    # 注册添加过滤模块
    INSTALLED_APPS = [
        ...,
        'django_filters',
        ...,
    ]
    # 添加过滤器,全局生效
    REST_FRAMEWORK = {
        ...,
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
    }

    ②视图添加DjangoFilterBackend过滤器

    新增 filter_backends 和 filterset_fields

    from django_filters.rest_framework import DjangoFilterBackend
    
    class ArticleListViewset(mixins.ListModelMixin,
                             mixins.CreateModelMixin,
                             viewsets.GenericViewSet):
        queryset = Article.objects.all()
        serializer_class = ArticleSerializer
        pagination_class = StandardResultsSetPagination
        filter_backends = [DjangoFilterBackend]
        filterset_fields = ['category', 'user']

    运行后在界面上出现了 过滤器 ,增加了 文章类别 和 文章作者 两个过滤字段,可以选择并且联合过滤。但是有缺点就是无法按照区间进行过滤,比如点击量

    ③使用filterset方法自定义过滤

    在blogs目录下新建filters.py文件,用来写过滤

    from django_filters import rest_framework as filters
    from .models import Article
    
    class ArticleFilter(filters.FilterSet):
        """
        文章的过滤类
        """
        click_min = filters.NumberFilter(field_name="click_num", lookup_expr='gte')
        click_max = filters.NumberFilter(field_name="click_num", lookup_expr='lte')
        favor_min = filters.NumberFilter(field_name='favor_num', help_text="最低收藏量", lookup_expr='gte')
        comment_min = filters.NumberFilter(field_name='comment_num', help_text="最低评论量", lookup_expr='gte')
        title = filters.CharFilter(field_name='title', help_text='按标题模糊查询', lookup_expr='icontains')
    
        class Meta:
            model = Article
            fields = ['category', 'tags', 'user', 'title', 'click_min', 'click_max', 'favor_min', 'comment_min']

    编辑blogs目录下的views.py,去掉filterset_fields,新增 filterset_class

    from blogs.filters import ArticleFilter
    
    class ArticleListViewset(mixins.ListModelMixin,
                             mixins.CreateModelMixin,
                             viewsets.GenericViewSet):
        queryset = Article.objects.all()
        serializer_class = ArticleSerializer
        pagination_class = StandardResultsSetPagination
        filterset_class = ArticleFilter

    运行打开过滤器后可以看到过滤条件都有了

    搜索功能实现

    使用SearchFilter方法,filter_backends新增SearchFilter,新增search_fields

    from rest_framework.filters import SearchFilter
    
    class ArticleListViewset(mixins.ListModelMixin,
                             mixins.CreateModelMixin,
                             viewsets.GenericViewSet):
        queryset = Article.objects.all()
        serializer_class = ArticleSerializer
        pagination_class = StandardResultsSetPagination
        filter_backends = [DjangoFilterBackend, SearchFilter]
        filterset_class = ArticleFilter
        search_fields = ['title', 'brief', 'content']

    搜索框输入 父亲 ,请求路径变成了 http://localhost:8000/api/v1/articles/?search=父亲

    排序功能实现

    使用OrderingFilter方法,filter_backends新增OrderingFilter,新增ordering_fields

    from rest_framework.filters import SearchFilter, OrderingFilter
    
    class ArticleListViewset(mixins.ListModelMixin,
                             mixins.CreateModelMixin,
                             viewsets.GenericViewSet):
         """文章列表页,分页,搜索,过滤,排序"""
        queryset = Article.objects.all()
        serializer_class = ArticleSerializer
        pagination_class = StandardResultsSetPagination
        filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
        filterset_class = ArticleFilter
        search_fields = ['title', 'brief', 'content']
        ordering_fields = ['click_num', 'favor_num', 'comment_num', 'add_time']

    运行后过滤器可以看到过滤器字段、搜索、排序功能

     转:http://www.zhangyanc.club/article/fiter-search/

    DRF过滤器文档:http://www.iamnancy.top/djangorestframework/Filtering/

    django-filters官方文档:https://django-filter.readthedocs.io/en/master/guide/rest_framework.html

  • 相关阅读:
    Tensorflow从入门到精通之——Tensorflow基本操作
    Tensorflow从入门到精通之——Tensorflow基本操作
    卷积神经网络概述-七月在线机器学习集训营手把手教你从入门到精通卷积神经网络
    梳理百年深度学习发展史-七月在线机器学习集训营助你把握深度学习浪潮
    K-means聚类 的 Python 实现
    我在 B 站学习深度学习(生动形象,跃然纸上)
    我在 B 站学机器学习(Machine Learning)- 吴恩达(Andrew Ng)【中英双语】
    Eclipse新建Java工程出现红色感叹号怎么解决?
    SQL Server 2012安装时报错,错误 0x80070422怎么解决?解决方法。
    Shift键的三个妙用!Word又现神操作!
  • 原文地址:https://www.cnblogs.com/gcgc/p/13932283.html
Copyright © 2011-2022 走看看