Search code examples
pythonxmlodoo-12

Hide field using group


I want to hide the field for some users who are not in the group. Field: phone, module: res_partner

For this, I overridden the field by adding a group:

from odoo import api, fields, models


class Partner(models.Model):
    _inherit = 'res.partner'

    phone = fields.Char(groups='cmz_security_contacts.field_invisible_from_contacts')

Created groups:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="0">
         <record id="group_contact_user" model="res.groups">
            <field name="name">User</field>
            <field name="category_id" ref="base.module_category_contacts"/>
            <field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
        </record>

        <record id="group_contact_manager" model="res.groups">
            <field name="name">Manager</field>
            <field name="category_id" ref="base.module_category_contacts"/>
            <field name="implied_ids" eval="[(4, ref('group_contact_user'))]"/>
            <field name="users" eval="[(4, ref('base.user_root'))]"/>
        </record>

        <record id="field_invisible_from_contacts" model="res.groups">
            <field name="name">Field invisible from contacts</field>
            <field name="users" eval="[(4, ref('base.user_root'))]"/>
        </record>
    </data>
</odoo>

"Normal" user with rights "Users". When I click some contact: Error: "Unknown field phone in domain" Why?

Edit:

  • Inheriting the sms.partner_form_send_sms_form_view

      <odoo>
          <record id="partner_form_send_sms_form_view_secure" model="ir.ui.view">
              <field name="name">res.partner.form.send.sms.secure</field>
              <field name="model">res.partner</field>
              <field name="inherit_id" ref="sms.partner_form_send_sms_form_view"/>
              <field name="priority">10</field>
              <field name="arch" type="xml">
                  <xpath expr="//button[@name=%(sms.send_sms_form_action)d]" position="attributes">
                         <attribute name="groups">cmz_security_contacts.field_invisible_from_contacts</attribute>
                  </xpath>
              </field>
          </record>
      </odoo>
    
  • Overriding phone and mobile fields to set groups attribute

      from odoo import api, fields, models
    
      class Partner(models.Model):
          _inherit = 'res.partner'
    
          phone = fields.Char(groups='cmz_security_contacts.field_invisible_from_contacts')
          mobile = fields.Char(groups='cmz_security_contacts.field_invisible_from_contacts')  
    

How to locate the mobile button in partner_form_send_sms_form_view?


Solution

  • I suppose you did not use the phone field in a domain.

    Many views are inheriting the partner view, the sms.partner_form_send_sms_form_view view adds a button (Send SMS) which uses the phone field in attrs (attrs="{'invisible':[('phone', '=', False)]}").

    Even if you change that, there is an error (TypeError: dict.record.phone is undefined) in the kanban view which checks for phone field value (t-if="record.phone.raw_value">), but you can simply avoid that by checking if the phone field exists before trying to get its value.

    You need to update the partner view according to your changes because you restricted the field access to the users of the given groups only.