Django DRF - 使用基本搜索过滤器进行多重 OR 搜索
Django DRF - using basic search filter to do an multiple OR search
我正在尝试使用基本的 Django 过滤器进行多重 OR 搜索。我已经尝试了以下网址,但它们只有 return 第一个结果
api/circuit/?search=AB5814765;AB827451;AB0923784
使用逗号 returns 也没有结果。
是否有任何语法可以显示内置的多条记录或做一些定制需要做的事情,我尝试了一些定制尝试,但它们都失败了
当我尝试执行以下操作时,会出现 404
api/circuit/ref_search/AB5814765|AB827451|AB0923784
@action(detail=False, methods=['get'], url_path='ref_search/(?P<refs>[^/.]+)/', permission_classes=[IsAdminUser])
def ref_search(self, request, refs, *args, **kwargs):
refs = refs.split('|')
query = Q()
for x in refs:
q = Q(circuit__ref_no=x)
query |= q
queryset = DeviceCircuitSubnets.objects.filter(query)
serializer = self.serializer_class(queryset, many=True)
return Response(data=serializer.data)
我也试过了
api/circuit/ref=AB5814765|AB827451|AB0923784
def get_queryset(self):
refs = self.request.query_params.get('ref', None)
if refs is not None:
refs = refs.split('|')
query = Q()
for x in refs:
q = Q(status=x)
query |= q
queryset = queryset.filter(query)
return queryset
我的看法:
class SiteCircuitDataROView(viewsets.ReadOnlyModelViewSet):
queryset = Circuit.objects.all()
serializer_class = CircuitSerializer
permission_classes = (IsAdminUser,)
filter_class = Circuit
filter_backends = (filters.SearchFilter,)
您可以这样做,使用 __in
并调用 api/circuit/?refs=AB5814765|AB827451|AB0923784
class SiteCircuitDataROView(viewsets.ReadOnlyModelViewSet):
queryset = Circuit.objects.all()
serializer_class = CircuitSerializer
permission_classes = (IsAdminUser,)
filter_class = Circuit
filter_backends = (filters.SearchFilter,)
@action(detail=False, methods=['get'], url_path='ref_search/', permission_classes=[IsAdminUser])
def ref_search(self, request, *args, **kwargs):
refs = self.request.get('refs', '').split('|')
queryset = DeviceCircuitSubnets.objects.filter(ref__in=refs)
serializer = self.serializer_class(queryset, many=True)
return Response(data=serializer.data)
像下面这样改变你的看法
class SiteCircuitDataROView(viewsets.ReadOnlyModelViewSet):
serializer_class = CircuitSerializer
permission_classes = (IsAdminUser,)
def get_queryset(self):
queryset = Circuit.objects.all()
search = self.request.GET.get('search', '').split(',')
if search:
query = Q()
for term in search:
query = query | Q(circuit__ref_no__icontains=term)
queryset = queryset.filter(query)
return queryset
现在,像下面这样发送请求url
api/circuit/?search=AB5814765,AB827451,AB0923784
我正在尝试使用基本的 Django 过滤器进行多重 OR 搜索。我已经尝试了以下网址,但它们只有 return 第一个结果
api/circuit/?search=AB5814765;AB827451;AB0923784
使用逗号 returns 也没有结果。
是否有任何语法可以显示内置的多条记录或做一些定制需要做的事情,我尝试了一些定制尝试,但它们都失败了
当我尝试执行以下操作时,会出现 404
api/circuit/ref_search/AB5814765|AB827451|AB0923784
@action(detail=False, methods=['get'], url_path='ref_search/(?P<refs>[^/.]+)/', permission_classes=[IsAdminUser])
def ref_search(self, request, refs, *args, **kwargs):
refs = refs.split('|')
query = Q()
for x in refs:
q = Q(circuit__ref_no=x)
query |= q
queryset = DeviceCircuitSubnets.objects.filter(query)
serializer = self.serializer_class(queryset, many=True)
return Response(data=serializer.data)
我也试过了
api/circuit/ref=AB5814765|AB827451|AB0923784
def get_queryset(self):
refs = self.request.query_params.get('ref', None)
if refs is not None:
refs = refs.split('|')
query = Q()
for x in refs:
q = Q(status=x)
query |= q
queryset = queryset.filter(query)
return queryset
我的看法:
class SiteCircuitDataROView(viewsets.ReadOnlyModelViewSet):
queryset = Circuit.objects.all()
serializer_class = CircuitSerializer
permission_classes = (IsAdminUser,)
filter_class = Circuit
filter_backends = (filters.SearchFilter,)
您可以这样做,使用 __in
并调用 api/circuit/?refs=AB5814765|AB827451|AB0923784
class SiteCircuitDataROView(viewsets.ReadOnlyModelViewSet):
queryset = Circuit.objects.all()
serializer_class = CircuitSerializer
permission_classes = (IsAdminUser,)
filter_class = Circuit
filter_backends = (filters.SearchFilter,)
@action(detail=False, methods=['get'], url_path='ref_search/', permission_classes=[IsAdminUser])
def ref_search(self, request, *args, **kwargs):
refs = self.request.get('refs', '').split('|')
queryset = DeviceCircuitSubnets.objects.filter(ref__in=refs)
serializer = self.serializer_class(queryset, many=True)
return Response(data=serializer.data)
像下面这样改变你的看法
class SiteCircuitDataROView(viewsets.ReadOnlyModelViewSet):
serializer_class = CircuitSerializer
permission_classes = (IsAdminUser,)
def get_queryset(self):
queryset = Circuit.objects.all()
search = self.request.GET.get('search', '').split(',')
if search:
query = Q()
for term in search:
query = query | Q(circuit__ref_no__icontains=term)
queryset = queryset.filter(query)
return queryset
现在,像下面这样发送请求url
api/circuit/?search=AB5814765,AB827451,AB0923784