如何在Odoo 8中使用fields_view_get?

时间:2022-08-28 20:29:34

I'm trying to make a field readonly depending on a condition. This condition is that the user who opens the form belongs to a specific group (that's why I can't use attrs or groups to manage this).

我正在尝试根据条件进行只读。这种情况是打开表单的用户属于特定组(这就是为什么我不能使用attrs或组来管理它)。

What I did, and I'm pretty close to my purpose, is to overwrite fields_view_get method, check the condition, and alter the field if necessary.

我所做的,我非常接近我的目的,是覆盖fields_view_get方法,检查条件,并在必要时更改字段。

@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)
    my_group_gid = self.env.ref(
        'my_module.my_group').id
    current_user_gids = self.env.user.groups_id.mapped('id')
    if view_type == 'form':
        if my_group_gid in current_user_gids:
            doc = etree.XML(res['arch'])
            the_fields = doc.xpath("//field[@name='my_field']")
            the_field = the_fields[0] if the_fields \
                else False
            the_field.set('readonly', '1')
            res['arch'] = etree.tostring(doc)
    return res

It seems to work well, because if I click on Fields View Get under the Developer Mode, the XML view code has been changed and I can see <field name="the field" attrs= ... readonly="1"/>. However, I always can edit the field.

它似乎运行良好,因为如果我点击开发者模式下的字段视图获取,XML视图代码已更改,我可以看到 。但是,我总是可以编辑该字段。

I can't understand this very well, because there are other modules in which fields are modified exactly like this and they work.

我无法理解这一点,因为还有其他模块,其中字段的修改完全像这样,它们工作。

Despite that, I keep trying and if instead of readonly attribute, I set modifiers in the following way, it works:

尽管如此,我一直在尝试,如果不是readonly属性,我按以下方式设置修饰符,它的工作原理:

the_field.set('modifiers', '{"readonly": true}')

The problem: I'm totally overwriting all the modifiers of the field (like attrs, required, etc). So I want to update the modifiers attribute, not overwrite it. What I do is:

问题:我完全覆盖了该领域的所有修饰符(如attrs,required等)。所以我想更新modifiers属性,而不是覆盖它。我所做的是:

modifiers = the_field.get('modifiers')

When I try to convert the string modifiers to a dictionary, I get an error. This is the value of modifiers:

当我尝试将字符串修饰符转换为字典时,我收到错误。这是修饰符的值:

{"invisible": [["is_company", "=", true]]}

Can any one explain me why the_field.set('readonly', '1') is not working properly or why I cannot convert to a dictionary the variable modifiers?

任何人都可以解释为什么the_field.set('readonly','1')不能正常工作或为什么我不能将变量修饰符转换为字典?

EDIT

编辑

Ok, that variable is JSON data, so I should have converted it to a dictionary in a good way like this:

好吧,那个变量是JSON数据,所以我应该把它转换成一个字典,好像这样:

import json
modifiers_dict = json.loads(modifiers)

Now my question is only the first one:

现在我的问题只是第一个问题:

Why can't I simply use the_field.set('readonly', '1') to manage my purpose?

为什么我不能简单地使用the_field.set('readonly','1')来管理我的目的?

1 个解决方案

#1


2  

This is what modifiers are

这就是修饰语

Point is, openerp uses the attrs attribute into the view to setup accordingly the modifiers attribute, after that the fields_view_get is called, thus if you just change attrs it wouldn't work, then you need to change modifiers as well when you are within fields_view_get.

点是,openerp使用attrs属性进入视图以相应地设置modifiers属性,之后调用fields_view_get,因此如果你只是更改了attrs它将无法工作,那么当你在fields_view_get中时你也需要更改修饰符。

#1


2  

This is what modifiers are

这就是修饰语

Point is, openerp uses the attrs attribute into the view to setup accordingly the modifiers attribute, after that the fields_view_get is called, thus if you just change attrs it wouldn't work, then you need to change modifiers as well when you are within fields_view_get.

点是,openerp使用attrs属性进入视图以相应地设置modifiers属性,之后调用fields_view_get,因此如果你只是更改了attrs它将无法工作,那么当你在fields_view_get中时你也需要更改修饰符。