如何设置仅针对组可编辑的字段?在odoo9

时间:2016-08-30 09:23:10

标签: odoo odoo-9

在hr考勤中,有一个名为“employee_id”的字段。

我想将此字段设置为仅适用于某个组(或只为其他组设置只读)。

例如,我想在“表单”视图中将“employee_id”字段设置为仅适用于“manager”组。

我已经扩展了考勤模块,我在扩展模块的XML中有了这段代码:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
    <record id="view_employee_readonly_custom" model="ir.ui.view">
        <field name="name">hr.attendance.form</field>
        <field name="model">hr.attendance</field>
        <field name="inherit_id" ref="hr_attendance.view_attendance_form"/>
        <field name="groups_id" eval="[(6,0,[ref('base.group_hr_manager')])]"/>
        <field name="arch" type="xml">
            <field name="employee_id" position="attributes">
                <attribute name="readonly">True</attribute>
            </field>
        </field>
    </record>
    </data>
</openerp>

使用此代码,hr_manager组的所有人都可以编辑该字段。这与我想要的相反。

为了达到这个目的,我需要修改什么?

已编辑:我已使用其他字段修改原始代码,以便更好地理解。

2 个答案:

答案 0 :(得分:3)

我找到了它!

首先,必须将字段定义为每个人只读。

<xpath expr="//field[@name='employee_id']" position="replace">
     <field name="employee_id" attrs="{'readonly':True}"/>
</xpath>

然后,我们继承了第一个自定义视图

<field name="inherit_id" ref="hr_attendance_extend.view_employee_readonly_custom"/>

最后,我们删除了管理员组的只读限制(group_hr_manager)

<field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager')])]"/>
<field name="arch" type="xml">
    <xpath expr="//field[@name='employee_id']" position="attributes">
        <attribute name="readonly">False</attribute>
    </xpath>
</field>

以下是最终代码:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
    <record id="view_employee_readonly_custom" model="ir.ui.view">
        <field name="name">hr.attendance.form</field>
        <field name="model">hr.attendance</field>
        <field name="inherit_id" ref="hr_attendance.view_attendance_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='employee_id']" position="replace">
                <field name="employee_id" attrs="{'readonly':True}"/>
            </xpath>
        </field>
    </record>

    <record id="view_employee_readonly" model="ir.ui.view">
        <field name="name">hr.attendance.form</field>
        <field name="model">hr.attendance</field>
        <field name="inherit_id" ref="hr_attendance_extend.view_employee_readonly_custom" />
        <field name="groups_id" eval="[(6, 0, [ref('base.group_hr_manager')])]"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='employee_id']" position="attributes">
                <attribute name="readonly">False</attribute>
            </xpath>
        </field>
    </record>
    </data>
</openerp>

答案 1 :(得分:1)

如果我没记错的话,Odoo中没有内置的方法可以让字段仅针对某个组进行编辑。

您可以通过向其添加组来使其可见或不可见。

如果你想根据一个组使字段可编辑,你需要创建一个与用户相关的新计算字段,并在字段上添加一个attrs,使其基于用户只读。

在你的情况下,你需要这样的东西:

在python中:

can_edit_name = fields.Boolean(compute='_compute_can_edit_name')

def _compute_can_edit_name(self):
  self.can_edit_name = self.env.user.has_group('base.group_hr_user')

在你的xml中:

<xpath expr="//field[@name='name']" position="before">
  <field name="can_edit_name" invisible="1"/>
</xpath>
<xpath expr="//field[@name='name']" position="attributes">
  <attribute name="attrs">{'readonly': ['can_edit_name', '=', False]}</attribute>
</xpath>

这意味着如果can_edit_name为True,则该字段将是可编辑的。

我还没有对它进行过测试,因此可能存在一些拼写错误,但是这应该会让你知道如何做到这一点!

祝你好运!

相关问题