Django-create 过滤器基于使用 Q 对象和 if elif 条件的每个字段

Posted

技术标签:

【中文标题】Django-create 过滤器基于使用 Q 对象和 if elif 条件的每个字段【英文标题】:Django-create filters based on each field using Q object and if elif condition 【发布时间】:2017-07-21 07:01:26 【问题描述】:

我正在使用 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://localhost:8000/filtrequest/ -d '"CompanyName":"c3","Subject":"subject2","class":"1"' -H "Content-Type: application/json"

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

【问题讨论】:

你能显示错误吗? 我已经编辑了输出...一些请求丢失了@The_Cthulhu_Kid 您的顺序错误 =) 最广泛的应该放在最后。 尝试更改顺序,看看是否仍然出现错误。 前三个 if 各有 2 个条件...第四个 if 有 3 个条件...按升序排列... 【参考方案1】:

尝试更改您的elifs 的顺序:

# This has to go first. If two of the conditions are fulfilled this will never be reached
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("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']))

【讨论】:

以上是关于Django-create 过滤器基于使用 Q 对象和 if elif 条件的每个字段的主要内容,如果未能解决你的问题,请参考以下文章

Django:使用 Q 对象对任意数量的输入进行动态过滤(OR & AND)

Django 结合两个没有 Q 的查询

F与Q查询

根据所有关联成员过滤记录

使用 Q 过滤 django 数据

使用重复参数而不是链接过滤器时,如何使用 Q 对象进行 AND 查找?