While using Odoo development, you often get a use case to extend a base menu and add some custom group to it to restrict the access. Assuming the xml way of adding attributes to field, we may tend to do the same menu items in Odoo, sadly this will not work. Below is the proper way to extend and add user groups to a base menu.
For example we have menu item - purchase.menu_product_pricelist_action2_purchase - and we have a custom user group that is - custom_module.group_access_price_suppliers - now we need to add this new user group to the menu item. In order to do that we shall create a security.xml in our custom module and add below code to that
<record id="purchase.menu_product_pricelist_action2_purchase" model="ir.ui.menu">
<field name="groups_id" eval="[(6, 0, [ref('ambala.group_access_price_suppliers')])]"/>
</record>
This code snippet is from an XML file in an Odoo module, and it's used to define a menu item in the Odoo user interface. Let's break down the code:
xml
Copy code
: This tag is used to define a new record in Odoo's database. In this case, it's defining a record for a menu item.
id="purchase.menu_product_pricelist_action2_purchase": This assigns a unique identifier to the record. This ID is used to reference this record from other parts of the code.
model="ir.ui.menu": Specifies the Odoo model for which the record is being defined. In this case, it's defining a menu item (ir.ui.menu).
: This line sets the groups_id field of the menu item record. The groups_id field is used to specify which user groups have access to this menu item.
eval="[(6, 0, [ref('ambala.group_access_price_suppliers')])]: This part evaluates to a Python expression that defines the access groups for this menu item.
(6, 0, [ref('ambala.group_access_price_suppliers')]): This is a tuple with three elements:
6: This indicates that the operation is to replace the existing records with the given list.
0: This is the ID of the record to be replaced. Since it's 0, it means it's creating a new record.
[ref('ambala.group_access_price_suppliers')]: This is a reference to a user group defined in the system. ambala.group_access_price_suppliers likely represents a user group called "Price Suppliers" defined elsewhere in the code. It specifies that members of this user group will have access to this menu item.
if we want to just add the custom user group but keeping all the existing group then do the below
<field name="groups_id" eval="[(4, ref('your_custom_group_id'))]"/>
Top comments (0)