Odoo:基于department_id,one2many域输入员工

时间:2017-03-08 08:36:27

标签: python openerp odoo-8

从这张图片here

当我点击添加新项目时,我希望显示的员工列表只是员工 从选定的部门来看,在图例中是“IT”部门。我使用odoo v8

我在我的o2m领域试过这个:

class overtime(models.Model):
    _name = 'overtime.overtime'

    department_id = fields.Many2one('hr.department', string='Department', readonly=True, states={'draft': [('readonly', False)]})
    employee_ids = fields.One2many('overtime.overtime_details', 'overtime_id', string="Employee", 
                                domain="[('hr.employee.department_id', '=', department_id)]")


class overtime_details(models.Model):
    _name='overtime.overtime_details'

    nik = fields.Char('NIK', size=250, required=True)
    overtime_id = fields.Many2one('overtime.overtime', string="Overtime", ondelete='cascade')
    job_id = fields.Many2one('hr.job', string="Position")
    employee_id = fields.Many2one('hr.employee', "Employee", required=True, select=True)

并得到这样的错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 544, in _handle_exception
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 581, in dispatch
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 317, in _call_function
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\service\model.py", line 118, in wrapper
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 314, in checked_call
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 810, in __call__
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\http.py", line 410, in response_wrap
  File "C:\Program Files (x86)\Odoo 8.0\server\openerp\addons\web\controllers\main.py", line 944, in call_kw
  File "C:\Program Files (x86)\Odoo 8.0\server\openerp\addons\web\controllers\main.py", line 936, in _call_kw
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\api.py", line 268, in wrapper
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\models.py", line 3148, in read
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\api.py", line 266, in wrapper
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\models.py", line 3183, in read
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\api.py", line 266, in wrapper
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\models.py", line 3370, in _read_from_database
  File "C:\Program Files (x86)\Odoo 8.0\server\.\openerp\osv\fields.py", line 774, in get
TypeError: cannot concatenate 'str' and 'list' objects

这是xml:

   <record id="overtime_form1" model="ir.ui.view">
            <field name="name">overtime_form1</field>
            <field name="model">overtime.overtime</field>
            <field name="arch" type="xml">
                <form string="Overtime Request Form">
                  <sheet>
                    <div class="oe_title">
                        <label for="code" class="oe_edit_only"
                            string="Overtime Code" />
                        <h1>
                            <field name="code" />
                        </h1>
                    </div>
                    <group>
                        <field name="department_id"/>
                        <label string="Duration"/>
                        <div>
                            <group col="5">
                                <field name="start_date" class="oe_inline"/>
                                <label string="-" class="oe_inline"/>
                                <field name="end_date" class="oe_inline"/>
                            </group>
                            </div>
                            <field name="total_overtime"/>
                            <field name="overtime_bonus"/>
                            <field name="reason"/>  
                        </group>
                        <notebook>
                            <page string="Employees">
                                <field name="employee_ids" >
                                    <tree string="Employees" editable="bottom">
                                        <field name="nik"/>
                                        <field name="employee_id"/>
                                        <field name="job_id"/>
                                    </tree>
                                </field>
                            </page>
                        </notebook>
                    </sheet>
                </form>
            </field>
        </record>

之前感谢:)

3 个答案:

答案 0 :(得分:1)

首先,您需要向overtime.overtime_details模型添加相关字段,以便像这样识别department_id

department_id = fields.Many2one(related="overtime_id.department_id", store=False)

然后,在xml视图中,您现在可以通过employee_id字段树中的department_id过滤employee_ids列表:

<notebook>
<page string="Employees">
    <field name="employee_ids" >
        <tree string="Employees" editable="bottom">
            <field name="department_id" invisible="1"/>
            <field name="nik"/>
            <field name="employee_id" domain="[('department_id', '=', department_id)]" />
            <field name="job_id"/>
        </tree>
    </field>
</page>
</notebook>

答案 1 :(得分:1)

您可以使用 parent.department_id

来完成
<notebook>
<page string="Employees">
    <field name="employee_ids" >
        <tree string="Employees" editable="bottom">
            <field name="nik"/>
            <field name="employee_id" domain="[('department_id', '=',parent.department_id)]" />
            <field name="job_id"/>
        </tree>
    </field>
</page>

这可能会对你有帮助。

答案 2 :(得分:0)

您可以使用employee_id

过滤domain中的员工
<notebook>
    <page string="Employees">
        <field name="employee_ids" >
            <tree string="Employees" editable="bottom">
                <field name="nik"/>
                <field name="employee_id" domain="[('department_id', '=', department_id)]" />
                <field name="job_id"/>
            </tree>
        </field>
    </page>
</notebook>