Search code examples
odooodoo-15

How to set specific fields as readonly for specific user in odoo 15?


Below given are fields of my model out of which half fields is for sales and half of them is for accounts. (I have mentioned by comment)

class emd_sales(models.Model):
_name = 'emd.sales'
_inherit = ['mail.thread', 'mail.activity.mixin']
_rec_name = 'tender_no'

#sales fields
date = fields.Date(string='Date', tracking=True, default=fields.Date.context_today)
company_id = fields.Many2one('emd.company', string='Company')
customer_id = fields.Many2one('res.partner', string='Customer')
product_id = fields.Many2one('product.template', string="Product", tracking=True)
product_desc = fields.Char(string='Product Description')
tender_no = fields.Char(string='Tender Number')
tender_due_date = fields.Date(string='Tender Due Date', tracking=True)
mode_of_sd = fields.Selection([('DD', 'DEMAND DRAFT'), ('BANK TRANSFER', 'BANK TRANSFER'), ('FDR', 'FDR'),
                               ('ONLINE TRANSFER', 'ONLINE TRANSFER'), ('CREDIT CARD', 'CREDIT CARD')],
                              required=True)  # status bar
amount = fields.Integer(string='Amount')

# accounts fields
bank_guarantee_no = fields.Integer(string='Bank Guarantee No.')
date = fields.Date(string='Date', tracking=True)
instrument_no = fields.Integer(string='Instrument Number')
bank_name = fields.Char(string='Bank Name')
period = fields.Selection([
    ('12 Months', '12 Months'),
    ('18 Months', '18 Months'),
    ('24 Months', '24 Months'),
    ('36 Months', '36 Months')
])

expiry_date = fields.Date(string='Expiry Date', tracking=True)
remarks = fields.Text(string='Remarks')

Below given is my form view :

<group>
                    <group>
                        <field name="date"/>
                        <field name="company_id"/>
                        <field name="customer_id"/>
                        <field name="product_id"/>
                        <field name="product_desc"/>
                        <field name="tender_no"/>
                        <field name="tender_due_date"/>
                        <field name="mode_of_sd" widget="radio"/>
                        <field name="amount"/>
                    </group>
                    <group>
                        <field name="bank_guarantee_no" groups="account.group_account_manager"/>
                        <field name="date" groups="account.group_account_manager"/>
                        <field name="instrument_no" groups="account.group_account_manager"/>
                        <field name="bank_name" groups="account.group_account_manager"/>
                        <field name="period" groups="account.group_account_manager"/>
                        <field name="expiry_date" groups="account.group_account_manager"/>
                        <field name="remarks" groups="account.group_account_manager"/>
                        <!--                        <field name="attachment" widget="many2many_binary"/>-->
                    </group>
                </group>
                

I wanted to make the sales fields as readonly for specific user.

As per the right answer I wrote the below code :

<record id="emd_form_view_inherit" model="ir.ui.view">
    <field name="name">emd.form.view.inherit</field>
    <field name="model">emd.accounts</field>
    <field name="inherit_id" ref="emd.emd_accounts_form_view"/>
    <field name="groups_id" eval="[(6, 0, [ref('account.group_account_manager')])]"/>
    <field name="arch" type="xml">
        <field name="date" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="company_id" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="customer_id" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="product_id" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="product_desc" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="tender_no" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="tender_due_date" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="mode_of_sd" widget="radio" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
        <field name="amount" position="attributes">
            <attribute name="readonly">1</attribute>
        </field>
    </field>
</record>

But when I login as admin , I am not able to edit those sales field. I want admin can edit all the fields.


Solution

  • You can extend the view and set the readonly attribute and define the groups allowed to use/access the current view.

    If the view extends an existing view, the extension will only be applied for a given user if the user has access to the provided groups_id.

    Example:

    <record id="emd_sales_form_view_inherit" model="ir.ui.view">
        <field name="name">emd.sales.form.view.inherit</field>
        <field name="model">emd.sales</field>
        <field name="inherit_id" ref="module_test.emd_sales_form_view"/>
        <field name="groups_id" eval="[(6, 0, [ref('module_test.group_sale_user')])]"/>
        <field name="arch" type="xml">
            <field name="date" position="attributes">
                <attribute name="readonly">1</attribute>
            </field>
        </field>
    </record>
    

    Odoo will apply the extension view and set the date field reaonly attribute to 1 if the current user belongs to module_test.group_sale_user group