【问题标题】:Django queryset If statement never evaluates to true?Django queryset If 语句永远不会计算为真?
【发布时间】:2018-10-29 06:32:14
【问题描述】:

我有一个简单的更新视图。从前一个视图读取 POST 请求。这部分效果很好。

owner = ADMirror.objects.get (employeentname=request.POST.get('userpost'))

我有一个查询集定义为:

currentlevel = QVReportAccess.objects.filter(ntname = 'owner.employeentname, active = 1).values('sr_datareduce_summary_code')

打印出来的样子:

<QuerySet [{'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_c
ode': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_sum
mary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_dataredu
ce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_da
tareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {
'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z0712
6'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code': 'Z07126'}, {'sr_datareduce_summary_code':
'Z07126'}, '...(remaining elements truncated)...']>

查询集将包含 sr_datareduce_summary_code 的重复项,因为它们位于模型中的各个程序级别。

然后我有以下 if 语句,如果为真,则为是,如果为假,则为否,但即使查询集包含 Z07126,我的 if 语句也永远不会计算为真。为什么会这样,我怎样才能让它正常工作?

if QVReportAccess.objects.filter(ntname = owner.employeentname, active = 1).values_list('sr_datareduce_summary_code') == "Z07126":
    appaccess = 'yes'
else:
    appaccess = 'no'

print(appaccess)

【问题讨论】:

  • 您将列表与字符串进行比较,这永远不会是真的
  • 看起来您正在将元组列表(来自values_list())与字符串进行比较。
  • 如何将列表的值与字符串进行比较?
  • 这是一个值列表,这意味着它包含许多项目,那么您想与Z07126比较哪个字段?

标签: python django django-queryset


【解决方案1】:

您正在将列表与字符串进行比较,这永远不会是真的

您只需要检查过滤器中的代码exists

if QVReportAccess.objects.filter(ntname = owner.employeentname, active = 1, sr_datareduce_summary_code= "Z07126").exists():

或者如果您需要保留values_list,请将其分配给一个变量并将invalues_list('sr_datareduce_summary_code', flat=True) 一起使用

if 'Z07126' in my_query_list:

【讨论】:

  • 这对我也有帮助 Sayse。谢谢。
【解决方案2】:
queryset = QVReportAccess.objects.filter(ntname=owner.employeentname, active=1).values_list('sr_datareduce_summary_code', flat=True)

if queryset.exists() and str(queryset[0]) == "Z07126":
    appaccess = 'yes'
else:
    appaccess = 'no'

print(appaccess)

所以filter() 基本上返回一个queryset 而不是一个对象,它也可能是空白的,检查查询集是否为空的最好方法是使用exists() values_list() 迭代时返回元组。每个元组都包含传递给它的相应字段的值。

如果只传入单个字段,也可以传入flat参数。如果为 True,这将意味着返回的结果是单个值,而不是一个元组。不通过flat=True时的例子:

>>> Entry.objects.values_list('id').order_by('id')
[(1,), (2,), (3,), ...]

当你通过flat=True:

>>> Entry.objects.values_list('id', flat=True).order_by('id')
[1, 2, 3, ...]

我希望你有解决问题的想法。

【讨论】:

  • 感谢 Sumeet 的解释。我们会深思熟虑的,你已经把我的问题弄清楚了。
【解决方案3】:

或者你可以只循环过滤后的查询集:

for obj in QVReportAccess.objects.filter(
    ntname=owner.employeentname, 
    active=1, 
    sr_datareduce_summary_code="Z07126"):

    # do something with obj
    print(obj.sr_datareduce_summary_code)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多