How to Remove Create, Edit, Delete, and Duplicate Options in Odoo Form View

Abid Patel
26-Sep-2024 Updated : 26-Sep-2024

Learn how to remove Create, Edit, Delete, and Duplicate options in Odoo form views. Follow this step-by-step guide to customize form actions, enhance data security, and streamline user permissions

How to Remove Create, Edit, Delete, and Duplicate Options in Odoo Form View

Odoo provides a highly flexible platform for businesses to manage various operations, and one of its powerful features is the form view, where users can create, edit, delete, or duplicate records. However, in certain scenarios, you might want to limit these options to maintain data integrity, streamline operations, or prevent unauthorized actions.

This blog will guide you through the process of removing the Create, Edit, Delete, and Duplicate options in Odoo form views. These options can be disabled for specific models or views, based on your requirements.

Why Remove Create, Edit, Delete, or Duplicate Options?

There are several reasons why you might want to restrict these options in Odoo:

  • ▹ Data Integrity: Limiting users from editing or deleting sensitive records ensures that important data remains unchanged.
  • ▹ User Roles: Certain user roles, like basic users or customers, might not require permissions to create or modify records.
  • ▹ Simplified Workflow: Restricting these actions can simplify the user interface and reduce confusion for end-users.

Step-by-Step Guide: Removing Create, Edit, Delete, and Duplicate Options

Below is a detailed step-by-step guide on how to remove these options in Odoo form views.

Step 1: Remove Create and Edit Options

The create and edit options can be disabled directly in the form view by setting the create and edit attributes to false.

Example XML Code:

xml

<record id="view_product_form" model="ir.ui.view">
    <field name="name">product.template.form</field>
    <field name="model">product.template</field>
    <field name="arch" type="xml">
        <form string="Product" create="false" edit="false">
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="default_code"/>
                </group>
            </sheet>
        </form>
    </field>
</record>
    

In the code above:

  • ▹ The create="false" attribute prevents the "Create" button from appearing.
  • ▹ The edit="false" attribute disables the ability to edit the record in the form view.

Step 2: Remove Delete Option

To remove the Delete option from the form view, you need to modify the access rights of the corresponding user groups or models. By setting delete to false, users will no longer be able to delete records from the form.

Example XML Code:

xml

<record id="view_product_form" model="ir.ui.view">
    <field name="name">product.template.form</field>
    <field name="model">product.template</field>
    <field name="arch" type="xml">
        <form string="Product" delete="false">
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="default_code"/>
                </group>
            </sheet>
        </form>
    </field>
</record>
    

The delete="false" attribute ensures that the "Delete" button is hidden in the form view.

Step 3: Remove Duplicate Option

Odoo's Duplicate option is part of the action dropdown that appears in the top right corner of form views. To remove the duplicate option, you will need to override the action linked to that button.

Example Python Code:

Python

from odoo import models, fields, api

class ProductTemplate(models.Model):
    _inherit = 'product.template'

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(ProductTemplate, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        if view_type == 'form':
            for action in list(res.get('toolbar', {}).get('action', [])):
                if action.get('name') == 'Duplicate':
                    res['toolbar']['action'].remove(action)
        return res
    

In this code:

  • ▹ The fields_view_get method is overridden to filter out the Duplicate option from the toolbar.
  • ▹ The "Duplicate" action is removed from the form toolbar by searching for the action with the name attribute and then deleting it.

Step 4: Combine All Restrictions

You can combine all of the above options to create a form view that restricts creating, editing, deleting, and duplicating records.

Example XML Code:

xml

<record id="view_product_form" model="ir.ui.view">
    <field name="name">product.template.form</field>
    <field name="model">product.template</field>
    <field name="arch" type="xml">
        <form string="Product" create="false" edit="false" delete="false">
            <sheet>
                <group>
                    <field name="name"/>
                    <field name="default_code"/>
                </group>
            </sheet>
        </form>
    </field>
</record>
    

By setting the attributes for create, edit, and delete to false, you effectively disable these actions from the form view.

Step 5: Apply Restrictions Based on User Groups

If you only want to restrict these options for specific users or groups, you can do so by creating access control lists (ACLs) and record rules that limit the permissions for certain groups.

Example Python Code:

python

from odoo import models, fields, api

class ProductTemplate(models.Model):
    _inherit = 'product.template'

    def _restrict_create_edit_delete(self):
        if self.env.user.has_group('sales_team.group_sale_salesman'):
            self.write({'create': False, 'edit': False, 'delete': False})
    

In this code:

  • The _restrict_create_edit_delete method checks whether the logged-in user is part of a specific group (in this case, the Salesman group), and if so, restricts the create, edit, and delete options for that group.

Best Practices for Disabling Form Options

  • ▹ User-Based Access: Use role-based access control to ensure that only authorized users can create, edit, or delete records. Restricting access per user group ensures data security and proper workflow management.
  • ▹ Critical Data: For sensitive data such as financial or customer records, it’s a good idea to restrict edit and delete options to prevent accidental data loss.
  • ▹ Simplicity: For simplified workflows, especially when dealing with external users or clients, disabling unnecessary options like duplicate and delete can help minimize confusion.

Conclusion

Disabling the Create, Edit, Delete, and Duplicate options in Odoo form views is a straightforward process that can help you control user permissions and protect data integrity. By following the steps outlined in this guide, you can customize your Odoo form views to meet your business needs while maintaining control over how records are managed.

Make a Comment

Your email address will not be published. Required fields are marked *