如何设置只对一个组可编辑的字段?在odoo9中
How to set a field editable only for a group? In odoo9
在 hr attendances 中,有一个名为 "employee_id" 的字段。
我想将此字段设置为仅对一个组可编辑(或对其他组设置为只读)。
例如,我想在 "form" 视图中将字段 "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 组之外的所有人都可以编辑该字段。这与我想要的相反。
为了实现这个我必须修改什么?
已编辑:我已经用不同的字段修改了原始代码,以便更好地理解。
如果我没记错的话,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', '!=', True)]}</attribute>
</xpath>
这意味着如果 can_edit_name 为 True,该字段将是可编辑的。
我还没有测试过,所以可能会有一些拼写错误,但这应该能让你知道如何去做!
祝你好运!
找到了!
首先,必须将字段定义为只读。
<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>
在 hr attendances 中,有一个名为 "employee_id" 的字段。
我想将此字段设置为仅对一个组可编辑(或对其他组设置为只读)。
例如,我想在 "form" 视图中将字段 "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 组之外的所有人都可以编辑该字段。这与我想要的相反。
为了实现这个我必须修改什么?
已编辑:我已经用不同的字段修改了原始代码,以便更好地理解。
如果我没记错的话,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', '!=', True)]}</attribute>
</xpath>
这意味着如果 can_edit_name 为 True,该字段将是可编辑的。
我还没有测试过,所以可能会有一些拼写错误,但这应该能让你知道如何去做!
祝你好运!
找到了!
首先,必须将字段定义为只读。
<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>