0

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.

2 Answers2

1

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

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • it works well. But I have one more issue , I have 3 users admin, accounts, sales for now when I login as a admin , I am not able to edit those accounts fields as I set for accounts user. I want admin can edit all the fields. I am adding my code above. – Dhiren Ahuja Jun 09 '22 at 06:41
  • I actually wanted to provide three different user access rights I have done for sales and accounts that when I login as sales it only shows me sales fields and when I login as accounts it shows me both but it not allows me to edit sales fields. But now I want third one that when I login as admin it let me edit both sales and accounts fields. – Dhiren Ahuja Jun 09 '22 at 12:42
  • Did you try to add the account's user to a new group and update the `groups_id`? – Kenly Jun 09 '22 at 13:14
  • I dont know how to do that. can u explain ?? – Dhiren Ahuja Jun 09 '22 at 13:27
  • Use a new group (create a new group or use an existing group to which the admin user does not belong) to restrict the account's user access to sale fields – Kenly Jun 09 '22 at 13:58
  • Ya I understood that but don't know how to find the xml id of group can u give me xml id of any existing group to which admin user does not belong...? and also tell me to how to assign that group to account user. – Dhiren Ahuja Jun 10 '22 at 05:43
  • Activate debug mode and go to settings -> Users & Companies -> Groups then click on the debug icon and choose View Metadata. If you look in the source code use the module name + the record id. – Kenly Jun 10 '22 at 10:37
  • No user belong to sale receipt group (`account.group_sale_receipts` ). In your code above you restricted account's managers to access sale fields, you can do the same to make the fields editable for admin users. – Kenly Jun 10 '22 at 10:39
  • You can find an example on how to link a group to user in [base](https://github.com/odoo/odoo/blob/a7f7233e0eae8ee101d745a9813cba930fd03dcb/odoo/addons/base/security/base_groups.xml#L25) module. – Kenly Jun 10 '22 at 11:01
-1

You have to extend that particular view and add the security groups. see the example below.

  <record id="xmlid" model="ir.ui.view">
        <field name="name">model.name</field>
        <field name="model">model.name</field>
        <field name="inherit_id" ref="modulename.view_xml_id"/>
        <field name="groups_id" eval="[(4, ref('base.group_user'))]"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='name']" position="attributes">
                <attribute name="readonly">1</attribute>
            </xpath>
        </field>
    </record>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 09 '22 at 03:25