使员工字段从配置字段(多对多字段)中变为只读。

huangapple go评论75阅读模式
英文:

Make employee field readonly from configured fields(many2many field)

问题

Here's the translated content you requested:

我正在使用Odoo版本16,之前我使用的是Odoo版本11,我想将我的代码从v11升级到v16。

我卡在一个方法上,我想要将一些字段设为只读,这些字段是在设置中定义的(many2many字段),针对特定的用户组(除了管理员组)。这段代码在v11上工作正常,但在v16上不起作用。

所以,有人可以帮助我解决这个问题吗?

我的代码如下:

class HrEmployeeResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    editable_fields = fields.Many2many('ir.model.fields', 'editable_fields_rel', 'model_id', 'id',
                                       string='Editable Field', domain="[('model_id', '=', 'hr.employee')]")

class CustomEmployee(models.Model):
    _inherit = 'hr.employee'

     def fields_view_get(self, view_id=None, view_type="form", toolbar=False, submenu=False):
        res = super(CustomEmployee, self).fields_view_get(view_id=view_id,
                                                          view_type=view_type, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(res['arch'])
 
        ###### 使字段对其他人不可见###########
        config_fields = self.env['ir.default'].get('res.config.settings', 'editable_fields')
        all_editable_fields = self.env['ir.model.fields'].browse(config_fields)
        rec_list = []
        for rec in all_editable_fields:
            rec_list.append(rec.name)
 
       ###### 使字段对其他人只读 ###########
        if not self.env.user.has_group('hr.group_hr_manager'):
            for field in res['fields']:
                if field not in rec_list:
                    for node in doc.xpath("//field[@name='%s']" % field):
                        node.set('readonly', '1')
                        node_values = node.get('modifiers')
                        modifiers = json.loads(node_values)
                        modifiers["readonly"] = True
                        node.set('modifiers', json.dumps(modifiers))
            res['arch'] = etree.tostring(doc)
        ####################
        return res

Please note that I've translated your code, but I haven't made any changes to its functionality. If you encounter issues with it in Odoo version 16, you may need to adapt it to the changes in the new version.

英文:

I am using odoo version 16, earlier I am using odoo version 11 and i want to upgrade my code from v11 to v16.

I am stuck at one method where I want to make some fields read-only from defined configured fields(many2many field in setting) for a specific group (except manager group). This code worked for v11 but not working on v16.

So, anyone can help me out with this problem?

My code is below:

class HrEmployeeResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    editable_fields = fields.Many2many('ir.model.fields', 'editable_fields_rel', 'model_id', 'id',
                                       string='Editable Field', domain="[('model_id', '=', 'hr.employee')]")

class CustomEmployee(models.Model):
    _inherit = 'hr.employee'

     def fields_view_get(self, view_id=None, view_type="form", toolbar=False, submenu=False):
        res = super(CustomEmployee, self).fields_view_get(view_id=view_id,
                                                          view_type=view_type, toolbar=toolbar, submenu=submenu)
        doc = etree.XML(res['arch'])
 
        ###### Make fields invisible for others###########
        config_fields = self.env['ir.default'].get('res.config.settings', 'editable_fields')
        all_editable_fields = self.env['ir.model.fields'].browse(config_fields)
        rec_list = []
        for rec in all_editable_fields:
            rec_list.append(rec.name)
 
       ###### Make fields Readonly for others ###########
        if not self.env.user.has_group('hr.group_hr_manager'):
            for field in res['fields']:
                if field not in rec_list:
                    for node in doc.xpath("//field[@name='%s']" % field):
                        node.set('readonly', '1')
                        node_values = node.get('modifiers')
                        modifiers = json.loads(node_values)
                        modifiers["readonly"] = True
                        node.set('modifiers', json.dumps(modifiers))
            res['arch'] = etree.tostring(doc)
        ####################
        return res

Thanks in advance.

答案1

得分: 1

In Odoo 16,fields_view_get 变成了 get_view

要使字段变为只读,重写 _get_view 函数,该函数返回一个 etree 节点。

示例:

class CustomEmployee(models.Model):
    _inherit = 'hr.employee'

    @api.model
    def _get_view_cache_key(self, view_id=None, view_type='form', **options):
        """The override of _get_view making the fields readonly for HR managers 
        makes the view cache dependent on the fact the user has the group HR manager or not"""
        key = super()._get_view_cache_key(view_id, view_type, **options)
        key = key + (self.env.user.has_group('hr.group_hr_manager'),)
        return key

    @api.model
    def _get_view(self, view_id=None, view_type='form', **options):
        arch, view = super()._get_view(view_id, view_type, **options)
        if not self.env.user.has_group('hr.group_hr_manager'):
            rec_list = [...]
            for field in arch.xpath("//field"):
                if field.get('name') not in rec_list:
                    field.set('readonly', '1')
        return arch, view
英文:

In Odoo 16 fields_view_get becomes get_view.

To make fields read-only override the _get_view function which returns the arch as an etree node

Example:

class CustomEmployee(models.Model):
    _inherit = 'hr.employee'

    @api.model
    def _get_view_cache_key(self, view_id=None, view_type='form', **options):
        """The override of _get_view making the fields readonly for HR managers 
        makes the view cache dependent on the fact the user has the group HR manager or not"""
        key = super()._get_view_cache_key(view_id, view_type, **options)
        key = key + (self.env.user.has_group('hr.group_hr_manager'),)
        return key

    @api.model
    def _get_view(self, view_id=None, view_type='form', **options):
        arch, view = super()._get_view(view_id, view_type, **options)
        if not self.env.user.has_group('hr.group_hr_manager'):
            rec_list = [...]
            for field in arch.xpath("//field"):
                if field.get('name') not in rec_list:
                    field.set('readonly', '1')
        return arch, view

huangapple
  • 本文由 发表于 2023年4月13日 18:15:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004268.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定