Odoo Qweb报表打印多个One2many字段出现NoneType下标错误如何解决
错误排查
你遇到的TypeError: 'NoneType' object is not subscriptable报错由多处语法和逻辑错误共同导致:
- 报表取值代码逻辑错误
name变量未定义就调用name.id作为查询条件,导致subjects查询结果异常甚至为空;doc_model配置错误,你报表是基于classroom.details模型生成,并非student.subject.lines;循环外打印info变量,当subjects为空时info未定义会直接触发报错。 - Qweb模板语法错误
t-foreach的t-as属性不能加[]后缀,t-as="info[]"写法非法导致变量解析异常;判断条件info['subject_line']=='line.subject_id'加了引号变成字符串比较,且二者是记录集对象不能直接这样比较。 - 冗余逻辑问题
class.room模型已经定义了student_subject_line关联字段,完全不需要额外在报表方法里查询拼装科目分数数据,直接在模板中调用关联字段遍历即可。
修复方案
1. 简化报表取值代码
from odoo import api, fields, models class StudentProgressReport(models.AbstractModel): _name = 'report.student_details.report_student_progress' @api.model def _get_report_values(self, docids, data=None): docs = self.env['classroom.details'].browse(docids) return { 'doc_model': 'classroom.details', 'docs': docs, }
2. 修正Qweb模板代码
<table> <tr> <td style="padding:5px 50px 0px 5px;background-color:#E0E0E0;"> <p style="text-align:right;"><strong>Teacher Name:</strong></p></td> <td style="padding:5px 50px 0px 5px;background-color:#E0E0E0;"> <t t-esc="doc.teacher_name"/> </td> </tr> </table> <br/> <table class="table table-condensed" style="border: 3px solid black !important;"> <tr t-foreach='doc.classroom_lines' t-as='line'> <td style="padding:5px 50px 0px 5px;background-color:#E0E0E0;" valign="top"> <p style="text-align:right;"><strong>Student Name:</strong></p></td> <td style="padding:5px 50px 0px 5px;background-color:#E0E0E0;"> <span t-esc="line.student_name.name"/> <!-- 直接遍历当前学生行关联的科目分数 --> <table class="w-100 mt-2"> <tr t-att-class="'bg-200 font-weight-bold o_line_section'" t-foreach="line.student_subject_line" t-as="info"> <td> <t t-esc="info.subject.name"/> </td> <td> <t t-esc="info.marks"/> </td> </tr> </table> </td> </tr> </table>
内容的提问来源于stack exchange,提问作者user16114985
相关产品推荐
相关产品推荐

