django-filter详解
过滤组件django-filter
官⽅⽂档:
1 安装
pip install django-filter
在django配置⽂件中注册app
INSTALLED_APPS = [
...
'django_filters',  # 需要注册应⽤,
]
环境需求:
Python: 3.6, 3.7, 3.8
Django: 2.2, 3.1, 3.2
DRF: 3.10+
2 使⽤
给出⼀个模型表:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=5, decimal_places=2)
description = models.TextField()
release_date = models.DateField()
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
模型表有很多字段,我们想按照价格和⽇期排序,可以这样做
import django_filters  # 导⼊模块
class ProductFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr='iexact')
class Meta:
model = Product
fields = ['price', 'release_date']
django-filter⽀持跨表操作,也可以使⽤双下划线查询,这样会使过滤类显得很冗长,但是保持了最⼤的⾃定义程度:
class ProductFilter(django_filters.FilterSet):
price = django_filters.NumberFilter()
price__gt = django_filters.NumberFilter(field_name='price', lookup_expr='gt')
price__lt = django_filters.NumberFilter(field_name='price', lookup_expr='lt')
release_year = django_filters.NumberFilter(field_name='release_date', lookup_expr='year')
release_year__gt = django_filters.NumberFilter(field_name='release_date', lookup_expr='year__gt')
release_year__lt = django_filters.NumberFilter(field_name='release_date', lookup_expr='year__lt')
manufacturer__name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Product
fields = ['price', 'release_date', 'manufacturer']
过滤器有两个主要参数:
field_name:字段名称,你可以使⽤django中的双下划线查询语法来使⽤
django前端模板lookup_expr:过滤时的过滤条件,⽀持双下划线语法
有关双下划线查询,
字段field_name和lookup_expr⼀起表⽰⼀个完整的Django查表达式。⽐如price__gt = django_filters.NumberFilter(field_name='price', lookup_expr='gt')定义之后,浏览器get请求携带参数price__gt=10,就会根据price字段过滤出符合条件价格⼤于10的数据。
在Meta类中,model指定要过滤的模型表,fields指定通过哪些字段来过滤。此外,可以使⽤字典为每个字段指定多个查表达式:
import django_filters
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = {
'price': ['lt', 'gt'],
'release_date': ['exact', 'year__gt'],
}
如果包含外键字段,同样可以使⽤双下划线语法:
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = ['manufacturer__country']
3 drf中使⽤
在drf中使⽤过滤:
from django_filters import rest_framework as filters
# 导⼊并继承FilterSet类
class ProductFilter(filters.FilterSet):
...
你的视图类还需要将DjangoFilterBackend添加到filter_backend中(局部配置)
from django_filters import rest_framework as filters
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_backends = (filters.DjangoFilterBackend,) # 配置filter_backends
filterset_fields = ('category', 'in_stock')
全局配置:
REST_FRAMEWORK = { # 全局配置
...
'DEFAULT_FILTER_BACKENDS': (
'st_framework.DjangoFilterBackend',
),
}
要使⽤FilterSet过滤,需要将其添加到视图类的filterset_class参数中。
from rest_framework import generics
from django_filters import rest_framework as filters
from myapp import Product
# 定义过滤类
class ProductFilter(filters.FilterSet):
min_price = filters.NumberFilter(field_name="price", lookup_expr='gte')
max_price = filters.NumberFilter(field_name="price", lookup_expr='lte')
class Meta:
model = Product
fields = ['category', 'in_stock']  # 在Meta中指定过滤哪些字段
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = ProductFilter  # 在filterset_class中,指定要使⽤的过滤类
由于⽀持双下划线语法,就可以在过滤类中创建区间过滤。⽐如ProductFilter中可以定义min_price和max_price,前端传⼊最⼩价格和最⼤价格就可以过滤出相应字段。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。