标签:完全 自定义 regex 技术 port pre 搜索 contains search
https://github.com/carltongibson/django-filter
https://django-filter.readthedocs.io/en/master/guide/rest_framework.html
https://django-filter.readthedocs.io/en/master/ref/filters.html
https://github.com/carltongibson/django-filter
pip install django-filter
INSTALLED_APPS = [
...
‘django_filters‘,
]
‘DEFAULT_FILTER_BACKENDS‘: (
‘django_filters.rest_framework.DjangoFilterBackend‘, # 过滤功能
‘rest_framework.filters.SearchFilter‘, # 搜索功能
‘rest_framework.filters.OrderingFilter‘, # 排序功能
),
filter_fields = (‘字段1‘,‘字段2‘)
queryset = Idc.objects.all()
serializer_class = IdcSerializer
# filter_backends = (DjangoFilterBackend,)
filter_fields = (‘name‘,‘letter‘)
from django_filters import rest_framework as filters
class ProductFilter(filters.FilterSet):
...
from django_filters import rest_framework as filters
class ProductList(generics.ListAPIView):
queryset = Product.objects.all()
serializer_class = ProductSerializer
filter_backends = (filters.DjangoFilterBackend,)
filterset_fields = (‘category‘, ‘in_stock‘)
from django_filters import rest_framework as filters
from idcs.models import Idc
from django.db.models import Q
class IdcFilter(filters.FilterSet):
class Meta:
model = Idc
fields = [‘name‘,‘letter‘]
queryset = Idc.objects.all()
serializer_class = IdcSerializer
# filter_backends = (DjangoFilterBackend,)
# filter_fields = (‘name‘,‘letter‘)
filter_class = IdcFilter
from django_filters import rest_framework as filters
from idcs.models import Idc
from django.db.models import Q
class IdcFilter(filters.FilterSet):
name = filters.CharFilter(field_name="name", lookup_expr=‘icontains‘)
class Meta:
model = Idc
fields = [‘name‘,‘letter‘]
变量是前端传过来搜索的 key
from django_filters import rest_framework as filters
from idcs.models import Idc
from django.db.models import Q
class IdcFilter(filters.FilterSet):
name = filters.CharFilter(field_name="name", lookup_expr=‘icontains‘)
contact = filters.CharFilter(method=‘by_contact‘)
def by_contact(self, queryset, name, value):
# name 是要搜索的key value是搜索的value
return queryset.filter(Q(phone__icontains=value)|Q(email__icontains=value))
class Meta:
model = Idc
fields = [‘name‘,‘letter‘,‘contact‘]
‘DEFAULT_FILTER_BACKENDS‘: (
‘django_filters.rest_framework.DjangoFilterBackend‘, # 过滤功能
‘rest_framework.filters.SearchFilter‘, # 搜索功能
‘rest_framework.filters.OrderingFilter‘, # 排序功能
),
filter_fields = (‘name‘,‘letter‘)
operators = {
‘exact‘: ‘= %s‘,
‘iexact‘: ‘LIKE %s‘,
‘contains‘: ‘LIKE BINARY %s‘,
‘icontains‘: ‘LIKE %s‘,
‘regex‘: ‘REGEXP BINARY %s‘,
‘iregex‘: ‘REGEXP %s‘,
‘gt‘: ‘> %s‘,
‘gte‘: ‘>= %s‘,
‘lt‘: ‘< %s‘,
‘lte‘: ‘<= %s‘,
‘startswith‘: ‘LIKE BINARY %s‘,
‘endswith‘: ‘LIKE BINARY %s‘,
‘istartswith‘: ‘LIKE %s‘,
‘iendswith‘: ‘LIKE %s‘,
}
from rest_framework import filters
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_class = GoodsFilter
search_fields = (‘name‘, ‘goods_brief‘, ‘goods_desc‘)
ordering_fields = (‘sold_num‘, ‘shop_price‘)
‘^‘ :以xx字符串开始搜索
‘=‘ :完全匹配
‘@‘ :全文搜索(目前只支持Django的MySQL后端)
‘$‘ :正则表达式搜索
from rest_framework import generics
class ArticleViewSet(generics.ListAPIView):
queryset = Article.objects.all() # 查询结果集
serializer_class = ArticleSerializer # 序列化类
pagination_class = ArticlePagination # 自定义分页会覆盖settings全局配置的
def get_queryset(self):
queryset = Article.objects.all()
read_num = self.request.query_params.get(‘read_num‘, 0) # 获取查询字段值
if read_num:
queryset = queryset.filter(read_num__gt=int(read_num))
return queryset
标签:完全 自定义 regex 技术 port pre 搜索 contains search
原文地址:https://www.cnblogs.com/jenvid/p/12927264.html