【问题标题】:Odoo 10 : Related field and inheritance from third parentOdoo 10:相关字段和来自第三父母的继承
【发布时间】:2017-05-13 09:37:58
【问题描述】:

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

假设我们有 4 个表:

  • 部门
  • 团队
  • Teams_employees
  • 员工

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

在部门视图中,我想显示其所有员工以及team_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,但这似乎有点奇怪。 有没有更好的解决方案来解决它? 任何帮助将不胜感激。非常感谢。

【问题讨论】:

  • 您想在表单上还是在树形视图上显示它们?
  • 你想只看它们还是想修改它们?
  • @ChesuCR 在树形视图中
  • @ChesuCR 只看到他们
  • 好的,看看我的回答有没有帮助

标签: python cakephp orm openerp odoo-10


【解决方案1】:

您可以创建自己的 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
        )""")

创建视图后,您只需像处理其他模型一样将其显示在树视图上

【讨论】:

  • 非常感谢,非常感谢您的帮助,我稍后会尝试:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
  • 2019-10-22
相关资源
最近更新 更多