Odoo 10:相关领域和第三方父母的继承

时间:2017-05-13 09:37:59

标签: python cakephp orm openerp odoo-10

我想知道是否有一种很好的方法来显示来自第三个父级的继承数据。为了澄清这个问题,我们举一个例子。

假设我们有4个表:

  • 部门
  • Teams_employees
  • 员工

每个部门都有许多团队(many2one关系),每个团队都包含许多员工(many2many),并在关系表teams_employees中包含其他一些有用的详细信息。

在部门视图中,我希望向所有员工显示teams_employees和employees所需的所有详细信息。我想知道是否有简单的ORM方法或简短的XML代码:

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

或者,如果有一个简单的方法,通过相关领域将部门通过其团队与所有员工联系起来。

我被困在这里两天,并没有找到像我以前在CakePhp中做的那样的解决方案,例如:

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

我尝试将id部门添加到Teams_employees但似乎有点奇怪。 有没有更好的解决方案来解决它? 任何帮助将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以创建自己的postgreSQL视图。检查此示例。我发现它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
        )""")

创建视图后,您只需要像在其他模型中一样在树视图中显示它