如何允许组的用户修改 Odoo 中表单的特定部分?

How to allow an user of a group to modify specific parts of a form in Odoo?

我创建了一个名为 accountant 的新组。例如,如果该组的用户打开 res.partner 表单,他必须能够阅读所有内容,但只能修改某些特定字段(选项卡 Accountancy 内的字段,例如例如)。

所以我把权限createwriteunlinkread设置为010, 1res.partner 模型中 accountant 组。

问题:如果我是 accountant 组的用户并且我转到 res.partner 的表单,我将看到 编辑按钮,如果我点击它,我将能够修改我想要的任何字段(我不应该,只有选项卡内的那些)。

所以我想复制菜单项(将属性 groups="accountant" 复制)和表单(将除选项卡内容之外的所有字段设为只读)。

问题:如果我是 accountant 群组的用户(accountant 在其 implied_ids列表),我将看到两个菜单项(一个采用普通形式,另一个采用只读字段的重复形式)。

是否可以创建一个菜单项,根据单击上述菜单项的用户组打开一组特定的视图?关于如何成功实施它的任何想法?

在odoo 中,您不能根据组将少数或部分字段设置为'readonly'。如果你需要这样做,你可以使用自定义模块'smile_model_access_extension'。

要在菜单上加载适当的视图,您可以创建 'ir.actions.act_window' (view_ids) 'ir.action' 字段的记录,您可以在其中指定要加载的视图的顺序和类型当执行菜单操作时。在您的情况下,您可以为您的操作指定特定的 'form view'。

我在谷歌上搜索了很多,在 Odoo 论坛上有很多人问同样的问题,但没有人给他们答案。

最后,我找到了这个解决方法,在我的例子中它完美地工作:每个模型中的方法 field_view_get,在返回要显示的视图之前获取 XML 视图。这意味着您可以通过 Python 代码以任何您想要的方式操作视图。

你只需要知道如何使用像lxml这样的库来修改带有XML代码的字符串变量。我把我的例子:

RES.PARTNER模型(这里我们必须使用lxml库中的etree

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
                    submenu=False):
    res = super(res_partner, self).fields_view_get(
        view_id=view_id, view_type=view_type, toolbar=toolbar,
        submenu=submenu)

    checking_obj = self.env['my.own.checkings']
    doc = etree.XML(res['arch'])

    if checking_obj.belongs_to_accountant_group():
        doc.set('create', 'false')
        doc.set('delete', 'false')
        doc.set('edit', 'true')

    if view_type == 'form':
        accounting_pages = doc.xpath("//page[@name='accounting']")
        accounting_page = accounting_pages[0] if accounting_pages \
            else False

        if accounting_page is not False:
            if checking_obj.belongs_to_accountant_group():
                all_fields = doc.xpath("//field")
                for field in all_fields:
                    if accounting_page not in field.iterancestors():
                        checking_obj.set_modifiers(field,
                                                   {'readonly': True, })

    res['arch'] = etree.tostring(doc)
    return res

AUXILIAR CUSTOMIZED模型(命名为MY.OWN.CHECKINGS)(这里必须使用json库)

def update_json_data(self, json_data=False, update_data={}):
    ''' It updates JSON data. It gets JSON data, converts it to a Python
    dictionary, updates this, and converts the dictionary to JSON data
    again. '''
    dict_data = json.loads(json_data) if json_data else {}
    dict_data.update(update_data)
    return json.dumps(dict_data, ensure_ascii=False)

def set_modifiers(self, element=False, modifiers_upd={}):
    ''' It updates the JSON modifiers with the specified data to indicate
    if a XML tag is readonly or invisible or not. '''
    if element is not False:  # Do not write only if element:
        modifiers = element.get('modifiers') or {}
        modifiers_json = self.update_json_data(
            modifiers, modifiers_upd)
        element.set('modifiers', modifiers_json)

def is_accountant(self):
    return self.env.ref(
        'my_module.group_accountant').id in \
        self.env.user.groups_id.mapped('id')

通过这种方式,您可以根据当前用户的组将某些字段设置为只读。