Skip to Content

How to Archive Any Record in Odoo

Sometimes, in Odoo, you may need to archive a record without deleting it completely. While some models in Odoo, like products, already support archiving, others, such as product categories, do not.

Products can be archived

In this blog, I will demonstrate how to enable archiving functionality for Product Categories. Although we’re focusing on product categories, this approach can be applied to any model where archiving is necessary.

In Odoo, the archiving functionality relies on a boolean field named active. We need to add the field active with default=True to the model that currently lacks archiving functionality.


Add the active Field to the Model

To enable archiving functionality, extend the product.category model in your custom module by adding the active field. Don't forget to include the product module as a dependency in your module's manifest file.

Here’s the Python code:

class ProductCategory(models.Model):
_inherit = "product.category"

active = fields.Boolean('Active', default=True)

Once this field is added, Odoo automatically enables the archive option in the list view for product categories. You can access it by selecting one or more records and clicking on the Actions button.


Enable Archiving in the Form View

Adding the active field to the model is not enough to enable archiving in the form view. To display the archive functionality in the form view, you can use the following XML code.

<record id="product_category_form_view_inherit_archive_record" model="ir.ui.view">
<field name="name">product.category.form.inherit.archive.record</field>
<field name="model">product.category</field>
<field name="inherit_id" ref="product.product_category_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='parent_id']" position="after">
<field name="active" invisible="1"/>
</xpath>
</field>
</record>


Add a Filter for Archived Records

To manage archived records, add a filter to the search view XML.

<record id="product_category_search_view_inherit_archive_record" model="ir.ui.view">
<field name="name">product.category.search.inherit.archive.record</field>
<field name="model">product.category</field>
<field name="inherit_id" ref="product.product_category_search_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='parent_id']" position="after">
<separator/>
<filter name="archived" string="Archived" domain="[('active', '=', False)]"/>
</xpath>
</field>
</record>


You can access the full source code for this implementation on GitHub here.