How to use fields_view_get in Odoo 8?

I am trying to create a field readonlydepending on the condition. This condition is that the user who opens the form belongs to a certain group (therefore, I cannot use attrsor groupsto manage this).

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

@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

This seems to work well, because if I click on View Fields in Developer mode, the XML view code will be changed and I will see <field name="the field" attrs= ... readonly="1"/>. However, I can always edit the field.

I can not understand this very well, because there are other modules in which the fields change in the same way as they do, and they work.

, , readonly modifiers , :

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

: (, attrs, required ..). modifiers, . :

modifiers = the_field.get('modifiers')

modifiers , . modifiers:

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

- , the_field.set('readonly', '1') modifiers?

, JSON, :

import json
modifiers_dict = json.loads(modifiers)

:

the_field.set('readonly', '1') ?

+4
1

,

, openerp attrs , , fields_view_get, , attrs, , , fields_view_get.

+2

Source: https://habr.com/ru/post/1615429/


All Articles