动态构造过滤器请求.GET

2024-05-23 15:04:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个表单,用户可以在其中选择几个选项,并通过一个查询来过滤结果。例如,用户可以在初始形式上选择一个或多个商业垂直,如零售、餐饮等,在第二个形式上可以选择云、本地等软件实现。。。以及更多其他字段来构造查询以筛选结果。表单的每个部分(行业、部署模型)上的值可能有值,也可能没有值

我让过滤器部分工作,但有些错误。例如,如果我提交的表单的“行业”部分的值是在返回的表单结果上设置的初始复选框是

[<django.db.models.query_utils.Q object at 0x7f6b60b854d0>] 

但如果我只在表单的最后一部分提交值,它就会正常工作并按预期返回结果。在

^{pr2}$
<form action="" method="get" id="filterform"><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='adsfasf' /></div>
<p><label for="id_industry_0">Industry:</label> <ul>
<li><label for="id_industry_0"><input type="checkbox" name="industry" value="8" id="id_industry_0" /> clothing</label></li>
<li><label for="id_industry_1"><input type="checkbox" name="industry" value="7" id="id_industry_1" /> food</label></li>
<li><label for="id_industry_2"><input type="checkbox" name="industry" value="5" id="id_industry_2" /> Apparel</label></li>
<li><label for="id_industry_3"><input type="checkbox" name="industry" value="9" id="id_industry_3" /> food truck</label></li>
<li><label for="id_industry_4"><input type="checkbox" name="industry" value="6" id="id_industry_4" /> Bar/Night Club</label></li>
</ul></p>
<p><label for="id_deployment_model_0">Deployment model:</label> <ul>
<li><label for="id_deployment_model_0"><input type="checkbox" name="deployment_model" value="4" id="id_deployment_model_0" /> On Premise</label></li>
<li><label for="id_deployment_model_1"><input type="checkbox" name="deployment_model" value="6" id="id_deployment_model_1" /> Software as a Service</label></li>
<li><label for="id_deployment_model_2"><input type="checkbox" name="deployment_model" value="8" id="id_deployment_model_2" /> server</label></li>
<li><label for="id_deployment_model_3"><input type="checkbox" name="deployment_model" value="5" id="id_deployment_model_3" /> Cloud</label></li>
<li><label for="id_deployment_model_4"><input type="checkbox" name="deployment_model" value="7" id="id_deployment_model_4" /> premise</label></li>
</ul></p>
<input type="submit" value="Submit" />
</form>

Tags: 用户nameid表单forinputmodelvalue
1条回答
网友
1楼 · 发布于 2024-05-23 15:04:25

您的问题似乎是结果可以包含两个可能的值。 Q对象或查询集的列表。 在software_list视图中,使用queryset覆盖结果,但仅当设置了部署模型参数时。 result = Software.objects.filter(reduce(operator.and_, result))缩进一次到多个。当只供应工业的时候就跳过了。在

例如:

    if 'deployment_model' in request.GET:
        ...
        result.append(depquery)

        result = Software.objects.filter(reduce(operator.and_, result))

应该是:

^{pr2}$

整个方法如下:

def software_list(request):

    if request.method == 'GET':
        result = []
        if 'industry' in request.GET and 'industry' is not None:
            formattedq = request.GET
            #QueryDict instance is immutable so we need to make a copy
            formattedq = formattedq.copy()
            formattedq = formattedq.getlist('industry')
            softquery = Q(industries__id__in=[ind for ind in formattedq])
            #debugformattedq = formattedq
            result.append(softquery)

        if 'deployment_model' in request.GET:
            formattedq = request.GET
            #QueryDict instance is immutable so we need to make a copy
            formattedq = formattedq.copy()
            formattedq = formattedq.getlist('deployment_model')
            depquery = Q(deploymentmodels__id__in=[ind for ind in formattedq])
            #debugformattedq = formattedq
            result.append(depquery)

        result = Software.objects.filter(reduce(operator.and_, result))
    else:
        SoftwareSearchForm()
    return render_to_response('software_list.html',
        {
        #'software_list':results,
        'SoftwareSearchForm':SoftwareSearchForm,
        'formattedq':result,
        },
        context_instance = RequestContext(request)
        )

    return render_to_response('software_list.html', {'SoftwareSearchForm':SoftwareSearchForm})

if语句中还有一个小缺陷:if 'industry' in request.GET and 'industry' is not None:,因为字符串“industry”永远不会是None。你可能是想用:

^{4}$

或简化(感谢乔·霍洛威):

if request.GET.get('industry') is not None:

相关问题 更多 >