搜索
SearchFilter可以搜索我们设置的参数
- 配置
from .custom_model_view_set import CustomModelViewSet
from django_filters.rest_framework import DjangoFilterBackend
from .custom_filter import GameFilter
from rest_framework import filters
class GameView(CustomModelViewSet):
queryset = Game.objects.all()
serializer_class = GameSerializer
filter_backends = (DjangoFilterBackend, filters.SearchFilter)
# 过滤
filter_fields = ('name', 'status')
filterset_class = GameFilter
# 搜索
search_fields = ("name", "status")
http://127.0.0.1:8000/api/v1/games/?search=天