小编İlk*_*aya的帖子

具有变量值的Django查询集

我正在 django 中为我的数据库编写动态过滤器,我在其中使用以下代码,其中有 2 个变量(p_type,s_type):

        p_type=[]
        s_type=[]
        query = request.GET.get("q")
        p_type =request.GET.get("p_type")
        s_type = request.GET.get("s_type")
        #messages.add_message(request, messages.INFO, p_type)
        #messages.add_message(request, messages.INFO, s_type)
        if query:
            queryset_find = queryset_list.filter(
                Q(FP_Item__contains=query))
            context = {'object_list': queryset_find}
            return render(request, 'index.html', context)
        elif p_type:
            queryset_find = queryset_list.filter(
                Q(p_type__contains=s_type))
            context = {'object_list': queryset_find}
            return render(request, 'index.html', context)
        else:
            context = {'object_list': queryset}
            return render(request, 'index.html', context)
Run Code Online (Sandbox Code Playgroud)

但是 django 在下面一行返回错误

Q(p_type__contains=s_type))
Run Code Online (Sandbox Code Playgroud)

我有动态单选按钮,其中 p_type 的值与我的数据库匹配,但即使我收到以下错误:

Exception Type: FieldError
Exception Value:    
Cannot resolve keyword 'p_type' into field. Choices are: ... (same …
Run Code Online (Sandbox Code Playgroud)

python django variables django-queryset

5
推荐指数
1
解决办法
1278
查看次数

标签 统计

django ×1

django-queryset ×1

python ×1

variables ×1