检查varible是否是queryset djang中的列表

2024-05-16 01:01:57 发布

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

我正在检查我的变量是否在我的查询集中。你知道吗

来自表单的变量:

def magazine_new(request):
    if form.is_valid():
        post.mag_no = request.POST.get('mag_no')

        post.cn1 = request.POST.get('cn1')
        post.cn2 = request.POST.get('cn2')
        post.cn3 = request.POST.get('cn3')
        post.cn4 = request.POST.get('cn4')

然后,我想检查一下这些中枢神经系统是否存在于本杂志中,否
我试过了,但没用。你知道吗

if Magazine.objects.filter(prodc_magt_no__in = [post.cn1,post.cn2,post.cn3,post.cn4] and mag_no=post.mag_no):
   form.save()
   return redirect('someview')

else:
   return HttpResponse('Dind't exist or match to this magazine no')

return render(request,'cnCreate.html',{'form':form})

出现错误“要解包的值太多(应为2)” 或者在顺序测试中,此查询不起作用。你知道吗

我的数据库表是:

 ID     WORK_YMD    LINE_NM  MODEL_CODE    MAG_NO             PRODC_MAGT_NO 
        118002  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4035
        118003  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4027
        118004  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4039
        118005  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4037
        118006  20191015    PBA-21F BN94-14806W A656MAF00001    BR10BN9414806WA656MAE4038

如何执行此查询以检查数据库中prodc\u magt\u no字段中是否存在这些Cns

刀库必须相同:邮政杂志编号=磁号


Tags: noformgetreturnrequestpostpbamag
3条回答

这是错误的语法:

Magazine.objects.filter(prodc_magt_no__in = [post.cn1,post.cn2,post.cn3,post.cn4] and mag_no=post.mag_no)

相反,它应该是:

Magazine.objects.filter(prodc_magt_no__in=[post.cn1,post.cn2,post.cn3,post.cn4], mag_no=post.mag_no)

您可以使用python的membership identities来检测项目是否在列表中。你知道吗

if post.mag_no in [post.cn1,post.cn2,post.cn3,post.cn4]:
    form.save()
   return redirect('someview')

else:
   return HttpResponse('Dind't exist or match to this magazine no')

return render(request,'cnCreate.html',{'form':form})

在过滤函数中不使用“and”。你知道吗

if Magazine.objects.filter(prodc_magt_no__in=[post.cn1,post.cn2,post.cn3,post.cn4], mag_no=post.mag_no):
    ...do something...

相关问题 更多 >