模板query字段进行模糊查询
This commit is contained in:
parent
7edef62d26
commit
006bbb1fc8
@ -14,6 +14,7 @@ from .utils import ApiResponse
|
|||||||
from .pagination import StandardResultsSetPagination
|
from .pagination import StandardResultsSetPagination
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from apps.user.authentication import CustomTokenAuthentication
|
from apps.user.authentication import CustomTokenAuthentication
|
||||||
|
from django.db.models import Q
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
class TemplateViewSet(viewsets.ModelViewSet):
|
class TemplateViewSet(viewsets.ModelViewSet):
|
||||||
@ -255,11 +256,13 @@ class TemplateViewSet(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
请求体参数:
|
请求体参数:
|
||||||
- mission: 任务类型
|
- mission: 任务类型
|
||||||
|
- query: 搜索关键词(可选,用于模糊查询标题和内容)
|
||||||
- page: 页码(可选,默认1)
|
- page: 页码(可选,默认1)
|
||||||
- page_size: 每页数量(可选,默认10)
|
- page_size: 每页数量(可选,默认10)
|
||||||
"""
|
"""
|
||||||
# 获取查询参数
|
# 获取查询参数
|
||||||
mission = request.data.get('mission', None)
|
mission = request.data.get('mission', None)
|
||||||
|
query = request.data.get('query', None)
|
||||||
page = request.data.get('page', 1)
|
page = request.data.get('page', 1)
|
||||||
page_size = request.data.get('page_size', 10)
|
page_size = request.data.get('page_size', 10)
|
||||||
|
|
||||||
@ -273,10 +276,21 @@ class TemplateViewSet(viewsets.ModelViewSet):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 构建查询集
|
# 构建查询集
|
||||||
|
queryset = self.get_queryset()
|
||||||
|
|
||||||
|
# 按任务类型筛选
|
||||||
if mission and mission != 'all':
|
if mission and mission != 'all':
|
||||||
queryset = self.filter_queryset(self.get_queryset().filter(mission=mission))
|
queryset = queryset.filter(mission=mission)
|
||||||
else:
|
|
||||||
queryset = self.filter_queryset(self.get_queryset())
|
# 按关键词模糊查询
|
||||||
|
if query:
|
||||||
|
queryset = queryset.filter(
|
||||||
|
Q(title__icontains=query) |
|
||||||
|
Q(content__icontains=query)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 应用其他过滤器
|
||||||
|
queryset = self.filter_queryset(queryset)
|
||||||
|
|
||||||
# 设置分页
|
# 设置分页
|
||||||
paginator = self.pagination_class()
|
paginator = self.pagination_class()
|
||||||
|
Loading…
Reference in New Issue
Block a user