简体   繁体   中英

Odoo 10 : Related field and inheritance from third parent

I'm wondering if there is a good way to display inherited data from the third parent. To clarify the question let's take an example.

Assume that we have 4 table:

  • Departments
  • Teams
  • Teams_employees
  • Employees

Each department have many teams ( many2one relation) and each team contain many employees ( many2many ) with some other useful details in the relation table teams_employees .

In the department view I want to display all its employees with all the details needed from teams_employees and employees. I'm wondering if there is a simple ORM method or a short XML code as :

<field name="name" string="Department Name">
<tree>
    <field name="team_ids">
       <field name="name" string="Name Team">
       <field name="teams_employees_ids">
          <field name="employees_id">
             <field name="name" string="Name Employee"/>
          </field>
          <field name="position" string="Position in this team"/>
       </field>
    </field>
</tree>

Or if there is a simple method with related field to relate the department through its teams to all the employees.

I'm stuck here for two day and didn't find a solution like what I'm used to do in CakePhp for example :

foreach($this->departement->teams as $team){
    //display data
    foreach($team->employees as $employee){
        //display data
    }
}

I tried to add the id department to Teams_employees but it seems a little bit weird. Is there a better solution to solve it ? Any help will be appreciated. Thanks a lot.

You can create your own postgreSQL view. Check this example. I found it here :

from odoo import api, fields, models, tools

class ReportEventRegistrationQuestions(models.Model):
    _name = "event.question.report"
    _auto = False

    attendee_id = fields.Many2one(comodel_name='event.registration', string='Registration')
    question_id = fields.Many2one(comodel_name='event.question', string='Question')
    answer_id = fields.Many2one(comodel_name='event.answer', string='Answer')
    event_id = fields.Many2one(comodel_name='event.event', string='Event')

    @api.model_cr
    def init(self):
        """ Event Question main report """
        tools.drop_view_if_exists(self._cr, 'event_question_report')
        self._cr.execute(""" CREATE VIEW event_question_report AS (
            SELECT
                att_answer.id as id,
                att_answer.event_registration_id as attendee_id,
                answer.question_id as question_id,
                answer.id as answer_id,
                question.event_id as event_id
            FROM
                event_registration_answer as att_answer
            LEFT JOIN
                event_answer as answer ON answer.id = att_answer.event_answer_id
            LEFT JOIN
                event_question as question ON question.id = answer.question_id
            GROUP BY
                attendee_id,
                event_id,
                question_id,
                answer_id,
                att_answer.id
        )""")

Once you have created the view you just need to show it on a tree view as you do with the rest of the models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM