DRF通过 url参数 来对数据进行一些排序或过滤的操作,rest-framwork提供了filters来满足这一需求
全局filter
1
| pip install django-filter
|
在 settings 里指定应用到全局的 filter
1 2 3
| REST_FRAMEWORK = { 'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend',) }
|
filter定义字段
filters.py
1 2 3 4 5 6 7 8 9 10 11 12
| from django_filters import FilterSet class UserRecordFilter(FilterSet): class Meta: model = UserRecord fields = ['source', 'source_id', 'url',] fields = { 'from_channel':['gt', 'lt', 'in', 'exact', 'range', 'isnull', 'icontains'] } exclude = ['from_channel', 'created_time'] together = ['from_channel', 'resume_id']
|
views.py
1 2 3 4 5 6 7 8
| class UserRecordViewSet(ModelViewSet): serializer_class = UserRecordSerializer queryset = UserRecord.objects.all() filter_class = UserRecordFilter filter_backends = (DjangoFilterBackend,)
|
viewset 的 filter
为 viewset 分别指定 filter,方法就是在定义 viewset 的时候定义一个名为 filter_backend 的类变量:
1 2 3 4
| class UserListView(generics.ListAPIView): queryset = User.objects.all() serializer = UserSerializer filter_backends = (filters.DjangoFilterBackend,)
|
默认的 filter
SearchFilter
1 2 3 4 5
| filter_backends = (filters.SearchFilter,) search_fields = ('username', 'email')
|
OrderingFilter
1 2 3 4 5
| filter_backends = (filters.OrderingFilter,) ordering_fields = ('created_time')
|
自定义 filter
自定义filter,只需要定义 filter_queryset(self, request, queryset, view) 方法,并返回一个queryset即可
1 2 3 4 5 6 7 8 9 10 11 12
| class NodenameFilter(filters.BaseFilterBackend): """ 根据 nodename 来筛选[nodename] """ def filter_queryset(self, request, queryset, view): nodename = request.QUERY_PARAMS.get('nodename') if nodename: return queryset.filter(nodename=nodename) else: return queryset
|
参考链接
https://django-filter.readthedocs.io/en/develop/index.html#