如何解决如何在 Odoo qwerb 报告中打印多个 One2many 字段
我需要打印主题和标记。但它没有迭代并显示这样的错误。 TypeError: 'nonetype' 对象不可下标。 我为此调用了 python 函数,并在此处添加了代码:
from odoo 导入 api、字段、模型 class StudentProgressReport(models.AbstractModel):
_name = 'report.student_details.report_student_progress'
@api.model
def _get_report_values(self,docids,data=None):
print("docids",docids)
subjects = self.env['student.subject.lines'].search([('subject_line','=',name.id)])
student_subject_line = []
for info in subjects:
vals={
'subject_line':info.subject_line,'subject':info.subject.name,'marks':info.marks
}
student_subject_line.append(vals)
print(info.subject_line)
print(info.subject.name)
print("subjects",subjects)
print("student_subject_line",student_subject_line)
return {
'doc_model': 'student.subject.lines','subjects': subjects,'student_subject_line':student_subject_line
}
xml 代码是:
<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></br>
<table class="table table condensed" style="border: 3px solid black !important;">
<t t-set="student_name" t-value="0"/>
<tr t-foreach='doc.classroom_lines' t-as='line'>
<td style="padding:5px 50px 0px 5px;background-color:#E0E0E0;">
<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"/>
<t t-foreach="student_subject_line" t-as="info[]">
<t t-if="info['subject_line']=='line.subject_id'">
<tr t-att-class="'bg-200 font-weight-bold o_line_section'">
<td>
<t t-esc="info['subject']"/>
</td>
<td>
<t t-esc="info['marks']"/>
</td>
</tr>
</t>
</t>
</td>
</tr>
</table>
和python代码是: classclassroom_details(models.Model): _name = 'classroom.details'
teacher_name = fields.Char('Name of the Teacher')
classroom_lines = fields.One2many('class.room','subject_id','Subject List')
班级教室(models.Model): _name = 'class.room'
subject_id = fields.Many2one('classroom.details')
student_name = fields.Many2one('student.details',string='Student Name')
student_subject_line = fields.One2many('student.subject.lines','subject_line')
class Student_Subject(models.Model): _name = 'student.subject.lines'
subject_line = fields.Many2one('class.room')
subject = fields.Many2one("subject.master",string='Subject')
marks = fields.Float ('Marks')
解决方法
如果您正在寻找打印数据的简单操作,请尝试此操作:
<table class="table table-bordered solid table-condensed o_main_table table-sm">
<thead>
<tr>
<th><strong>Product Description</strong></th>
</tr>
</thead>
<tbody>
<tr t-foreach="o.move_ids_without_package" t-as="line">
<td>
<span t-field="line.product_id.name"/>
</td>
</tr>
</tbody>
</table>
你也可以应用python,比如在qweb本身中搜索或过滤数据。
<tr t-foreach="o.move_ids_without_package.search([('id','=',some_id )])" t-as="line">
或
<tr t-foreach="o.move_ids_without_package.filtered(lambda l: some condition to filter lines)" t-as="line">
,
- 在这个类中编写一个方法。我假设您的报告数据来自Student.Subject.Lines?
class classroom_details(models.Model):
_name = 'classroom.details'
- 在上述类中创建以下方法以收集您想要在报告中显示的所有数据。
def getAllStudentSubjectData(self):
# create a list of dict to store all the data you want to display in the report
std_data = []
subjects = self.env['student.subject.lines'].ids
# loop through each subjects
for sbj in subjects:
# empty dict
hello = {}
one_sbj = self.env['student.subject.lines'].search([('id',sbj.id)])
hello['mark'] = one_sbj.marks
hello['subject'] = one_sbj.subject
std_data.append(hello)
return std_data
- 在您的报告中,在进入循环之前调用此方法。
在报告模板中,我假设doc是student.subject.lines对象。
getAllStudentSubjectData() 返回字典列表,即 [{'mark':100,'subject': 'history'},{'mark' :90,'subject': 'english'},... ]。
因此,将字典列表放在这样的变量中,并在每次迭代中循环。
在每次迭代中,您像这样调用每个 subject_line 。
<t t-set="subject_lines" t-value="doc.getAllStudentSubjectData()"/>
<t t-foreach="subject_lines" t-as="sbj_line">
<tr t-att-class="'bg-200 font-weight-bold o_line_section'">
<td>
<t t-esc="sbj_line['subject']"/>
</td>
<td>
<t t-esc="sbj_line['mark']"/>
</td>
</tr>
</t>
</t>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。