如何在odoo中使用on2和many2one(正式开放)

时间:2018-02-23 03:36:43

标签: python odoo erp

1.我有两个字段,如“员工姓名”和“部门”。如果我在第一个字段中更改员工姓名,则第二个字段(“部门”)应该自动填充。请提供.py和。 XML Thanx提前

1 个答案:

答案 0 :(得分:0)

请尝试此代码以供参考:

<强>的.py

class HR(models.Model):
    _name = 'hr.hr'

    employee = fields.Many2one('emp.emp', string="Employee")
    department = fields.Many2one('emp.dept',related='employee.department',string="Department") 
    #related attribute will automatically change as you change employee and fill the department field


# Master of Employee
class Employee(models.Model):

    _name = 'emp.emp'

    name = fields.Char('Employee Name')
    department = fields.Many2one('emp.dept',string="Department")

# Master of Department
class Department(models.Model):

    _name = 'emp.dept'

    name = fields.Char('Department Name')

<强> .XML

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="employee_form_view" model="ir.ui.view">
        <field name="name">hr.hr.form</field>
        <field name="model">hr.hr</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="HR">
                <sheet>
                    <group>
                        <group>
                            <field name="employee"/>
                        </group>
                        <group>
                            <field name="department"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>
</odoo>

<!-- Write Tree View, Action, and Menu Code on your own -->
<!-- Also create view xml file for Employee Master and Department Master to enter Data -->