在Odoo中使用模型B中的M2O关系获取模型A的值

时间:2018-04-15 17:27:25

标签: model views relationship odoo

我有两个模型employeeemployee包含emp_nameemp_pnum列。

我在employeeres_users之间有一个Many2One关系,如代码所示。

class Employee(models.Model):
_name = "employee"
emp_name = fields.Char(string='Employee name')
emp_pnum = fields.Char(string='Phone number')
user_id = fields.Many2one('res.users', string='user id', default=lambda self: self.env.uid, required = True)

在表单视图中,我希望在表单视图中的res.users模型中显示employee模型字段旁边的字段。

<record model="ir.ui.view" id="employee_tree">
        <field name="name">employee.tree</field>
        <field name="model">employee</field>
        <field name="priority" eval="8" />
        <field name="arch" type="xml">
            <tree string="Employee">
                <field name="user_id" readonly="1"/>
                <field name="emp_name"/>
                <field name="emp_pnum"/>

//// I want to add the field values from the res.users table
//// through the Many2One relationship here

            </tree>
        </field>
    </record>

1 个答案:

答案 0 :(得分:1)

请在您的代码中尝试此逻辑:

模特A:

model_A1 = fields.Char()
model_desc = fields.Char()

模特B:

model_A1_child = fields.Many2one('modelA')
model_A1_desc = fields.Char(related='model_A1_child.model_desc')