https://github.com/Coxhuang/django-DjangoFilterBackend.git
Django==2.0.7 djangorestframework==3.8.2 django-filter==2.0.0
. ├── app │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── filters.py # 添加新文件 │ ├── migrations │ ├── models.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── djangofilters │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── manage.py └── templates
INSTALLED_APPS = [ ... 'rest_framework', 'django_filters', 'app', ]
from django.db import models class Teacher(models.Model): """老师表""" name = models.CharField(verbose_name="老师姓名",max_length=16) class Student(models.Model): """学生表""" tea = models.ForeignKey(Teacher,on_delete=models.DO_NOTHING,verbose_name="老师") name = models.CharField(verbose_name="学生姓名",max_length=16)
class getUserListSerializer(DynamicFieldsMixin,serializers.ModelSerializer): teaname = serializers.CharField(label="老师姓名",source="tea.name") class Meta: model = models.Student fields = ["id","name","teaname",] class getUserListView(mixins.ListModelMixin,GenericViewSet): queryset = models.Student.objects.all() serializer_class = getUserListSerializer
filters.py
import django_filters from app import models class getUserListFilter(django_filters.rest_framework.FilterSet): class Meta: model = models.Student fields = ["name",]
views.py
... from django_filters import rest_framework from app.filters import getUserListFilter class getUserListView(mixins.ListModelMixin,GenericViewSet): ... filter_backends = (rest_framework.DjangoFilterBackend,) filter_class = getUserListFilter
需求:根据老师的名字过滤
class getUserListFilter(django_filters.rest_framework.FilterSet): teaname = django_filters.CharFilter(field_name='tea__name', label="老师姓名") # 跨表操作 class Meta: model = models.Student fields = ["name","teaname",]
createDate = models.DateTimeField(verbose_name="用户创建时间",auto_now_add=True)
filters.py
class getUserListFilter(django_filters.rest_framework.FilterSet): teaname = django_filters.CharFilter(field_name='tea__name', label="老师姓名") RegDate = django_filters.DateFromToRangeFilter(field_name='createDate', lookup_expr='gte', label='注册时间') # 区间过滤 class Meta: model = models.Student fields = ["name","teaname","RegDate",]
注意 : 在url的参数中,原来我们在filters.py中定义的变量是 “RegDate”,到了url中变成了 “RegDate_after” 和 “RegDate_before”,这是框架给我设定好的区间变量,直接使用就行
more : 更多关键词,请参考文档
需求:学生老师的薪资范围过滤
filters.py
class getUserListFilter(django_filters.rest_framework.FilterSet): teaname = django_filters.CharFilter(field_name='tea__name', label="老师姓名") RegDate = django_filters.DateFromToRangeFilter(field_name='createDate', lookup_expr='gte', label='注册时间') salary = django_filters.RangeFilter(method='salary_filter', label='薪资',) class Meta: model = models.Student fields = ["name","teaname","RegDate","salary"] def salary_filter(self, queryset, name, value): return queryset.filter(Q(tea__salary__gte = int(value.start)) & Q(tea__salary__lte = int(value.stop)))
salary = django_filters.RangeFilter(method='salary_filter', label='薪资',)
def salary_filter(self, queryset, name, value): return queryset.filter(Q(tea__salary__gte = int(value.start)) & Q(tea__salary__lte = int(value.stop)))