user16114985
user16114985

Reputation:

How to print multiple One2many fields in Odoo qwerb report

I need to print subjects and marks.but its not iterated and Showing an error like this. TypeError: 'NoneType' object is not subscriptable. I am calling python function for that and the code is added here:

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):
    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 code is:

                                <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> 

                            

and python code is: class classroom_details(models.Model): _name = 'classroom.details'

teacher_name = fields.Char('Name of the Teacher')
classroom_lines = fields.One2many('class.room', 'subject_id','Subject List')

class classroom(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')  

I am new in odoo.please help me to solve this.

Upvotes: 0

Views: 874

Answers (2)

Khay Leng
Khay Leng

Reputation: 451

  1. Write a method in this class. I assume your report's data is from Student.Subject.Lines?
class classroom_details(models.Model): 
_name = 'classroom.details'
  1. Create the following method in the above class to collect all the data you want to show in your report.
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
  1. In your report, call this method before go into looping.

    Here in the report template, I assume doc is student.subject.lines object.
    getAllStudentSubjectData() returns a list of dict ie [{'mark':100, 'subject': 'history'},{'mark':90, 'subject': 'english'},... ].

    So put the list of dicts in a variable like this and loop through each iteration.

    Inside each iteration, you call each subject_line like this .
<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> 

Upvotes: 0

Himanshu Sharma
Himanshu Sharma

Reputation: 716

Please try this if you're looking for simple operation of printing the data:

<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>

You can also apply python such as searching or filtering the data in qweb itself.

<tr t-foreach="o.move_ids_without_package.search([('id', '=', some_id )])" t-as="line">

or

<tr t-foreach="o.move_ids_without_package.filtered(lambda l: some condition to filter lines)" t-as="line">

Upvotes: 1

Related Questions