django 3.2.13
解释:过滤其实就是应用于ListModelMixin其为所有内容对其进行过滤的,需要专门安装注册,如127.0.0.1:8000/books/?gid=88
,在所有内容里面得到gid为88的内容
安装:pip install django-filter
配置:
INSTALLED_APPS = [
...
'django_filters', # 需要注册应用,
]
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}
# 不写入到setting.py里面,写入到views.py视图里,写法如下
# filter_backends = DjangoFilterBackend
使用:
class TestGenericAPIView(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
filterset_fields = ['id' ,'time', 'foreign_id']
filter.py
解释:exact表示精确查询,icontains表示模糊查询(都在列表表示都可以用)
用法:精确-http://127.0.0.1:8000/tea/?passwd=3543
;模糊-http://127.0.0.1:8000/tea/?passwd__icontains=35
import django_filters
from testWeb.models import Student
class UserFilter(django_filters.FilterSet):
class Meta:
model = Student
fields = {
"passwd": ['exact','icontains'],
"time": ['exact'],
}
views.py
class TestGenericAPIView(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
filterset_class= UserFilter
解释:gt表示大于、lt表示小于、gte表示大于等于和lte表示小于等于
用法:http://127.0.0.1:8000/tea/?time__lt=446660
import django_filters
from testWeb.models import Student
class UserFilter(django_filters.FilterSet):
class Meta:
model = Student
fields = {
"passwd": ['exact','icontains'],
"time": ['exact','gte','lte'],
}
解释:in表示在列表
用法:http://127.0.0.1:8000/tea/?time__in=446654,334534534
import django_filters
from testWeb.models import Student
class UserFilter(django_filters.FilterSet):
class Meta:
model = Student
fields = {
"passwd": ['exact','icontains'],
"time": ['exact','in'],
}
字段:
CharFilter 字符串类型
BooleanFilter 布尔类型
DateTimeFilter 日期时间类型
DateFilter 日期类型
DateRangeFilter 日期范围
TimeFilter 时间类型
NumberFilter 数值类型,对应模型中IntegerField, FloatField, DecimalField
属性:
field_name: 过滤字段名,一般应该对应模型中字段名
lookup_expr: 查询时所要进行的操作,和ORM中运算符一致
method:指定某个方法去运算,值为函数名
用法:http://127.0.0.1:8000/tea/?path=7
import django_filters
from django_filters import filters
from testWeb.models import Student
class UserFilter(django_filters.FilterSet):
path = filters.CharFilter(field_name='id', lookup_expr='gt')
class Meta:
model = Student
fields = {
"passwd": ['exact','icontains'],
"time": ['exact','gt','lt','in','isnull'],
}
用法:http://127.0.0.1:8000/tea/?abc=8
import django_filters
from django_filters import filters
from testWeb.models import Student
class UserFilter(django_filters.FilterSet):
abc = filters.NumberFilter(field_name='id', method='abcd')
def abcd(self,queryset, name,value):# queryset是对应着view里面的那个,name是调用该函数处理的字段,value是用户提交的值
value = value +1
e = Student.objects.filter(id=value)
return e
解释:其是使用过滤的方法,其使用rest_framework过滤里面的排序
使用:http://127.0.0.1:8000/tea/?ordering=-id # -号表示逆序,不加符号表示正序
class TestGenericAPIView(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
filter_backends = [OrderingFilter] # 这里不配置,就需要去setting.py里面配置
ordering_fields = ('id', )
基础分页类:
http://127.0.0.1:8000/tea/?page=1&page_size=2# page表示第几页,page_size表示一页显示几个
http://127.0.0.1:8000/tea/?limit=2&offset=2 # limit表示每次查多少,offset表示隔开几个开始查
使用方法:
解释:如果配置全局就无需专门写入局部配置,同理配置局部无需专门写入全局,理由(django在APIView父类里面默认会读取全局配置的,如果局部写相当于覆盖全局配置)
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100 # 每页数目
}
from rest_framework.pagination import PageNumberPagination
class LargeResultsSetPagination(PageNumberPagination):
page_size = 2
page_size_query_param = 'page_size'
max_page_size = 2
class TestGenericAPIView(ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
pagination_class = LargeResultsSetPagination