自定义过滤器
pip install django-filter
filter.py
class TaskFilter(FilterSet):
name = filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Task
fields = ['name', ]
settings.py
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
view.py
import django_filters.rest_framework
from django.contrib.auth.models import User
from myapp.serializers import UserSerializer
from rest_framework import generics
class TaskViewSet(BasicViewSet):
queryset = Task.all()
serializer_class = TaskSerializer
filter_class = filter.TaskFilter