Django python ValueError:没有足够的值来解包(预期值为2,实际值为1)

2024-09-21 03:27:58 发布

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

在我的views.py中将导出到excel的代码中,我在一个循环中合并了两个查询,但是我收到了这个错误Exception Type: ValueError at /export_reportF/ Exception Value: not enough values to unpack (expected 2, got 1)

    reports = TrEmployeeSuppliersFeedbackQuestionsSubmittedRecords.objects.filter(
        fmCustomerID__company_name__in=company.values_list('fmCustomerID__company_name')).order_by('-inputdate')
    daily = FmCustomerEmployeeSupplier.objects.filter(id__in=reports.values_list('fmCustomerEmployeeSupplierID'))
    pairs = [reports, daily]

    for report, day in pairs:
        writer.writerow(
            [
                smart_str(report.id),
                smart_str(report.dateSubmitted),
                smart_str(report.fmCustomerEmployeeSupplierID),
                smart_str(day.fmCustomerLocationID),
                smart_str(day.contact_number),
                smart_str(day.fmCustomerSectionID),
                smart_str(day.bodyTemperature),
                smart_str(report.q1Answer),
                smart_str(report.q2Answer),
            ]
        )

    return response

这是回溯Exception Type: ValueError at /export_reportF/ Exception Value: not enough values to unpack (expected 2, got 1)

Traceback:

File "C:\Users\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\Desktop\ContractTracingProject\TracingSettings\TracingApp\views.py" in export_reportF
  1301.     for report, day in pairs:

Exception Type: ValueError at /export_reportF/
Exception Value: not enough values to unpack (expected 2, got 1)

Tags: inpyreportvaluesmarttypeexceptionnot
1条回答
网友
1楼 · 发布于 2024-09-21 03:27:58

第3行是

pairs = [reports, daily]

这将提供一个包含两个列表的列表。如果您循环它,它将循环两次,首先给您报告列表,然后是每日列表。试试这个:

for item in pairs:
    print(item)
    print(type(item))

这应该让问题变得显而易见。解决方案是将3号线替换为:

pairs = zip(reports, daily)

这将为您提供一个iterable of pairs,其中每对都有一份报告和一天

相关问题 更多 >

    热门问题