Odoo 9 有没有办法在表单视图的某个字段上处理不同组的授权?

Odoo 9 Is there a way to handle authorization with different groups on a certain field in form view?

我正在尝试创建表单视图。

<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>

但是有很多属性,比如groups和不可见的权限相关,让特定的一群人可以看到这个字段。

groups="base.group_hr_user"

但是有没有办法让某些组可以编辑字段而其他组不能?

首先,您不能使用这样的域名

<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>

没有 '==' 运算符,请改用 =

现在,回答你的问题,如果你想为另一个组创建一个特殊视图,其中一些元素对于一个组是只读的,而在另一个组中是可编辑的,你必须这样。

对于默认视图:

<record id="some_model_view" model="ir.ui.view">
    <field name="name">some.model.form</field>
    <field name="model">some.model</field>
    <field name="arch" type="xml">
        <form>
             <field name="some_field" readonly="1"/>
        </form>
    <field/>
</record>

对于某个群体:

<record id="some_model_view_for_other_group" model="ir.ui.view">
    <field name="name">some.model.form</field>
    <field name="model">some.model</field>
    <field name="inherit_id" ref="my_module.some_model_view"
    <field name="groups_id" eval="[(6, 0, [ref('some.first_group')])]" />
    <field name="arch" type="xml">
        <field name="some_field" position="attributes">
            <attribute name="readonly">0</attribute>
        </field>
    <field/>
</record>

添加一个新字段来检查用户是经理还是用户。

新Api方法

check_user = fields.Boolean(string='user',compute='_compute_user_check')

@api.multi
def _compute_user_check(self):
    if self.user_has_groups('purchase.group_purchase_manager'):
        self.check_user =True

可见

<field name="is_positive" attrs="{'readonly':[('check_user','=','True')]}"/>

我将通过一个示例来说明此功能在销售组中的工作原理。

我将销售订单行中的单价字段设为只读我们select用户组user:own documents only 对于其他 2 个组 user:All documetsmanager

,该字段是 可编辑的

首先我创建了一个布尔字段来检查用户属于哪个组

is_own_user = fields.Boolean(string="Own user", compute='compute_own_user')

然后分配布尔字段是True当用户属于组user:own只文件否则分配给False

@api.depends('product_id')
def compute_own_user(self):
    res_user_id = self.env['res.users'].search([('id', '=', self._uid)])
    for rec in self:
        if res_user_id.has_group('sales_team.group_sale_salesman') and not res_user_id.has_group('sales_team.group_sale_salesman_all_leads'):
            rec.is_own_user = True
        else:
            rec.is_own_user = False

在xml中使is_own_user不可见并替换单价字段

<xpath expr="//notebook/page/field[@name='order_line']/tree/field[@name='price_unit']" position="replace">
    <field name="price_unit" attrs="{'readonly': [('isown_user', '=', True)]}" />
</xpath>