odoo中如何根据不同组设置字段可编辑?

admin 2019-8-14 5430

方法一:

First, the field has to be defined as readonly for everyone.

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

Then, we inherit the first custom view

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

Finally, we delete the readonly restriction for the administrators group (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>

Here is the final code:

<?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>


=========================

方法二:

If I remember correctly, there is no built-in way in Odoo to make a field editable for a certain group only.

You can make it visible or invisible by adding a group to it.

If you DO want to make the field editable depending on a group, you will need to create a new computed field that is user-dependent, and add an attrs on the field to make it readonly based on the user.

In your case, you would need something like this:

In 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')

In your 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>

This means that if the can_edit_name is True, the field will be editable.

I haven't tested it so there may be some typos, but this should give you an idea of how to do it !

Good luck !


最新回复 (0)
返回