Django使用Q对象和ifif条件创建基于每个字段的过滤器

时间:2017-07-21 07:01:27

标签: python django filter qobject

我正在使用Q对象,我希望得到来自companyName,CompanyUrl,Subject,Class字段的所有输入组合的结果。 请为Q对象建议if else条件的正确代码。

url.py

url(r' ^ filtrequest / $',views.FiltRequest.as_view())

view.py

class FiltRequest(APIView):
  def post(self, request, format=None):
    request.data['PubIp'] = getUserIP(request)
    returnData = ApiFiltReq.filtRequest(self, request.data, format=None)
    if returnData == "RECORD_NOT_FOUND":  # bad request
        return Response(ErrorCodes.ERROR_CODE_LIST[returnData], status=status.HTTP_400_BAD_REQUEST)
    elif returnData == "DJANGO_ENTRY_FAILED":
        return Response(ErrorCodes.ERROR_CODE_LIST[returnData], status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(returnData, status=status.HTTP_202_ACCEPTED)

ApiFiltReq.py的代码

 def filtRequest(request)
    if (Q("CompanyName" in request) & Q("CompanyUrl" in request) & Q("Subject" in request) & Q("Class" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(CompanyUrl=request['CompanyUrl']) | Q(
             Subject=request['Subject']) | Q(Class=request['Class']))

    elif (Q("CompanyName" in request)& Q("CompanyUrl" in request)& Q("Subject" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(CompanyUrl=request['CompanyUrl']) | Q(
                Subject=request['Subject']))
    elif (Q("CompanyName" in request)& Q("CompanyUrl" in request)&Q("Class" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(CompanyUrl=request['CompanyUrl']) | Q(Class=request['Class']))

    elif (Q("CompanyName" in request)&Q("Subject" in request)&Q("Class" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(Subject=request['Subject']) | Q(Class=request['Class']))

    elif (Q("CompanyUrl" in request)&Q("Subject" in request)&Q("Class" in request)):
        queryset = Product.objects.filter(
            Q(CompanyUrl=request['CompanyUrl']) | Q(Subject=request['Subject']) | Q(Class=request['Class']))


    elif (Q("CompanyName" in request)& Q("CompanyUrl" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(CompanyUrl=request['CompanyUrl']))

    elif (Q("CompanyName" in request)& Q("Subject" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(Subject=request['Subject']))

    elif (Q("CompanyName" in request)& Q("Class" in request)):
        queryset = Product.objects.filter(
            Q(CompanyName=request['CompanyName']) | Q(Class=request['Class']))

    elif (Q("CompanyUrl" in request)& Q("Subject" in request)):
        queryset = Product.objects.filter(Q(CompanyUrl=request['CompanyUrl']) | Q(
                Subject=request['Subject']))

    elif (Q("CompanyUrl" in request)&Q("Class" in request)):
        queryset = Product.objects.filter(Q(CompanyUrl=request['CompanyUrl']) | Q(Class=request['Class']))

    elif (Q("Subject" in request)& Q("Class" in request)):
            queryset = Product.objects.filter(Q(Subject=request['Subject']) | Q(Class=request['Class']))

    elif ("CompanyName" in request):
        queryset = Product.objects.filter(CompanyName=request['CompanyName'])

    elif ("CompanyUrl" in request):
        queryset = Product.objects.filter(CompanyUrl=request['CompanyUrl'])

    elif ("Subject" in request):
        queryset = Product.objects.filter(Subject=request['Subject'])
    elif ("Class" in request):
            queryset = Product.objects.filter(Class=request['Class'])

    serializer = ProductSerializer(queryset,many=True)
    for item in serializer.data:
       sendData.append({"ProductName":item['ProductName']})

关于运行命令...... curl -X POST http://docs.oracle.com/javafx/2/css_tutorial/jfxpub-css_tutorial.htm -d' {" CompanyName":" c3"," Subject":" subject2&#34 ;,"类":" 1"}' -H" Content-Type:application / json"

它提供了错误的请求,并且在服务器上打印e给出:[' CompanyUrl'] 该怎么做才能避免呢?

1 个答案:

答案 0 :(得分:0)

尝试并更改>> format debug >> x = rand(3) x = Structure address = 7fed6d1cada0 m = 3 n = 3 pr = 7fecdd3353e0 pi = 0 0.9649 0.9572 0.1419 0.1576 0.4854 0.4218 0.9706 0.8003 0.9157 的顺序:

elif