How to Empty a Model In Odoo / Mass Delete in Odoo / Mass Unlink

In Odoo, we use unlink() ORM method to delete a record. But if you are trying to empty a model or wants to delete multiple records, the best way to do it, is to use two steps.

First, search the records:

record_set = self.env['your.model'].search([])

Second, unlink them all at once instead of looping through them:


If you have a list of ids, search them using the list:

ids = [1, 2, 3, 10, 11, 12]
record_set = self.env['your.model'].search([('id', 'in', ids)])

and unlink:


Remember, there is no need to loop through this iterable object, odoo unlink does it for you.

TIPS: If you want to get all the ids in a model directly in a list, you can use the following:

record_list = self.env['your.model'].search([]).ids

How to Hide a Column Dynamically in a Parent Model in Tree View on Odoo

We all know how to hide a column in a model based on parent value matching in Odoo installation. I have already written a post on this here:

How to hide a column ‘dynamically’ in Tree View on Odoo

Above case works when you are trying a hide a column in a sale order line, or move line or invoice line. But what if, you want to hide a column in stock picking delivery tree view or the receipt view? The above method, will not work, because column_invisible only works when the parent is referenced. But in a tree view list like ‘Deliver Orders’ under Inventory Overview, if you want to hide a column based on condition, how do we do that?

The answer is, we use data from context and set the attribute ‘invisible’ to True based on the context value. We can either do that by using any existing context value or we can add a context value using a computed field property and match it in the xml.

I will discuss on how can we use the existing context key:value pair in hiding a column on stock.picking model.

First, let’s imagine, we have a character field on stock.picking called ‘woocommerce_id’.

class stock_picking(models.Model):
_inhert = 'stock.picking'

woocommerce_id = fields.Char(string="Woocommerce ID")

Now the xml for Inventory overview stock.picking trees would be inheriting stock.vpicktree

<record id="inherit_delivery_picking" model="ir.ui.view">
<field name="name"></field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.vpicktree"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='location_dest_id']" position="after">
<field name="woocommerce_id"/>

Now, the above will show woocommerce_id in all vpicktree view, like ‘Delivery Orders’, ‘Receipts’, ‘Internal Transfers’. But What if, you want to show this field to only Delivery Orders?

To do that, we need to know the value for ‘default_picking_type_id’. This value is automatically set on stock.picking model view in the context by Odoo (Hints: You may view all the keys available to context in a model, by creating a computed field, that writes self.env.context.keys() to a file or show it in the tree view to find the keys, and self.env.context.get(‘key_name’) to find the value for it).

If you check the different values set by that key for different pages, you can see, it is set to 1 for delivery orders, 3 for receipts and 5 for internal transfers. Now if you want to show the woocommerce_id field to only delivery receipts, we set the attribute invisible for that field when the context key value is not 1 like following:

<field name="woocommerce_id" invisible="context.get('default_picking_type_id') != 1"/>

Save it, upgrade the module, and see the magic! cheers!

How to Update Context in Odoo

You may want to pass some data to a specific page in Odoo, and change the fields based on those data. In those cases, you want to use Context. Context is a data structure passed to the request object that contains hidden values, and can be used to change results of the page.

Context is a frozendict data type in Odoo. That’s why you can change it like you do in a dict data type, for example:

'test': 'test_value',

As the Context is frozendict, it won’t take such changes. Odoo provides a way to change values, it’s called ‘with_context’. The syntax is as following:

self = self.with_context({
'test': 'test_value',

This one should rewrite the context available in the self object by adding the new key:value pair you have mentioned. There are times, this might not work as expected, and you want a patch technique to update the context. This can be done by changing the data type to dict.

self.env.context = dict(self.env.context)
'test': 'test_value',

This would also add the new key:value pair to your context and work as expected. There is almost no security concern here for converting the frozendict as the context will destroy once the page is left soon enough.

How to Validate Odoo Invoice Automatically

You might want to validate your odoo invoices automatically, without bothering your users to go to each vendor/customer bill and validate them, instead triggering based on certain conditions. It’s a 4 step code:

First, we get the invoice:

invoice_id = self.env['account.invoice'].browse(your_invoice_id)

Assign the date to it:


Create the invoice moves:


Now you can validate the invoice:


How to Add Invoice to Purchase Order in Odoo Automatically

I was facing an issue in two way matching algorithm for Incoming Receipts & Vendor bills in my Odoo installation. As vendor bills are tagged in Purchase Orders only through use of ‘Create Bill’, I had to make an algorithm to calculate the matching. Remember, the match would only work if the supply chain members follows the right way of doing it, otherwise, it would fail. That’s what happened, it fails almost 3 times out of 10. Then I realized, the invoice needs to be pinned automatically in the stock.picking model, instead of purchase.order model at the time a Receipt is validated, to keep the tracking for my matching.

While doing so, first challenge is to pin an invoice to a purchase id. By default, the model for invoicing, ‘account.invoice’ has a foreign read only key for purchase, which is purchase_id. But remember, this is a read only field, and can’t be written/created in a new or existing record using record. I dig down the Odoo codes a little under purchase/models/ and could find the Odoo function that can do the job for us. It’s called ‘purchase_order_change()’.

Here is a snippet of adding an invoice automatically to a purchase ID:

invoice_id = self.env['account.invoice'].create({
'type': 'in_invoice',


This will create an invoice in Draft state. Remember, you need to validate this invoice, if you would like to create the invoice number, not just an invoice entry. You may do it manually or you may do it automatically ‘How to Automatically Validate an Invoice in Odoo

Remember, I am triggering the snippet from the stock.picking, not from purchase.order, which is why, the ‘self’, is related to stock.picking and calling self.purchase_id to link the original purchase_id that triggered the stock.picking record.

Now, a small tip. What if, if you want to align the invoice to only have the products you have received in this particular receipt? Easy as pie:

for invoice_line in invoice_id.mapped('invoice_line_ids'):
check = 1
for move_line in self.mapped('move_line_ids'):
if ==
check = 0

if check:

Simple! Isn’t it? Happy coding!

How to hide a column ‘dynamically’ in Tree View on Odoo

Odoo field has an attribute called ‘attrs’. This adds custom attribute to the Odoo form/tree views. You may use different kind of attributes with attrs like ‘required’, ‘readonly’ or ‘invisible’. For basic, to hide a field regardless of it be an individual entity or parent relation, we use the following:

<field name='field_name' invisible='1'/>

This is useful when we are triggering a computed field that we don’t want to show on a tree/form view. But what if we want to dynamically decide whether to invisible the field or not? For such cases, we usually use invisible with the ‘attrs’ attribute on fields, for example:

<field name='product_id' attrs="{ 'invisible': [('is_set', '=', True)]}"/>

You might set the field 'is_set' itself as invisible and a boolean computed field like the following:

is_set = fields.Boolean(compute="_set_is_set")

def _set_is_set(self):
if self.lot_id.life_date:
self.is_set = True
self.is_set = False

Seems easy, right? But there is a catch, the above won’t hide the whole column if you are on a place like ‘purchase.order.line’ or ‘stock.move.line’. How can you work on such cases? It only works on individual entry, but not on a relational column. There is an attribute called ‘column_invisible’ same like ‘invisible’ we have used above. But the difference is, you need to set this based on parent value, thus hides a relational column. Here is an example to hide a column called ‘show_expiry’ in a picking operation:

<field name="show_expiry" attrs="{ 'column_invisible' : [('parent.picking_type_code', 'in', ['incoming'])]}" />

Above code checks the ‘picking_type_code’ value from ‘stock.picking’ model, and hides the column if it’s an incoming shipment and shows when it’s an outgoing shipment. That means, the above, would show the column (show_expiry) if it’s a Delivery to customer location or you already have the product, but won’t show if you are doing a GRN, means you don’t have the stock yet, just arriving.

Pretty simple, isn’t it? Good luck.

How to use dynamic ‘domain’ in Odoo 12 Many2one relation

There are two ways you can have a model data selection in Odoo, one is to use Selection field and the Other is Many2one relational field. To dynamically create a Selection field, all you need to do is to pass the list of (value, string) tuple as the first parameter or use a reference of a function to get a return list of same. Creating dynamic selection field is quite simple, with one drawback! It won’t work on api onchange. As the values on the selection fields are already loaded, api onchange can not reverse and reload the selection fields like it can do on ‘relational objects’. The only way left is to use a Many2one relational field.

Now Many2one relational field now make it a bit complicated as, you can’t generate the list in a straightforward way like you can do in selection field. Most likely, you would like to pass a domain, that matches some attribute of ‘self’, and some from another relation. Here is how you can achieve this!

I will create a simple model with two fields, one is Char and the other is Many2one:

_name = 'my.product.manager'

sku = fields.Char(string='SKU', required=True)
basket = fields.Many2one('basket.location', string='Product Basket Location', required=True)

Now, for example, you would like to select these SKUs during the creation of purchase order, and would like the field to come up when a change is made on the purchase order line.

First, we create a new Many2one relation field on ‘purchase.order.line’ model in relation to ‘my.product.manager’

_inherit = 'purchase.order.line'
basket_location_id = fields.Many2one( 'my.product.manager', 'Basket Location')

Cool, now you don’t want to share all the baskets in the purchase order form for a product, instead you want to show only those baskets that are allocated for that specific SKU. Here is how, you can pass a dynamic domain to the basket_location_id and filter it

def onchange_basket(self):
res = {
'domain' : {
'basket_dest_id' : [('sku', '=', self.product_id.default_code)],
return res

Neat! Pretty straight forward, isn’t it? Odoo 12 doesn’t give you the opportunity to choose a predefined location for a product. If you combine change.qty with the above, you can soon have a great module to use to let Odoo choose a product from default location. Cool Idea?

Happy coding!

How to Add Custom Sale Order Line in Odoo 12

There are two ways you can add custom sale order line in Odoo 12.

First Method

You can search the sale order first with the name or browse with the ID

sale_order = self.env['sale.order'].search([('name', '=', 'SO009')])

Now, you can see the sale order line IDs using the following:


As this is a One2many relation, we need to use a tuple in the Odoo ORM write() method. Here is the list of data you can pass in a tuple to change data that has One2many or Many2many relation

Odoo ORM Documentation

You can now pass this as python dictionary for ‘order_line’ to sale.order of desired product as following:

'order_line': [
(0,0, {
'product_id': 2003,
'price_unit': 3000.0,
'product_uom_qty': 2.0,
'name': ''

Second Method

In this method, you can simply define the order dictionary, and create a sale.order.line item with the order_id in reference. Here is an example

# define dictionary to add order_line
order_line_dict = { 'order_id' : 9, 'product_id': 2003, 'price_unit': 3000.0, 'product_uom_qty': 2.0, 'name': '' }

# search the order line for the sale order
sale_order_line = self.env['sale.order.line'].browse(9)

# create a new order line

# print sale order lines


If you are doing/testing this on the Odoo Shell, then make sure to run the following after the changes to reflect on database:

Odoo doesn’t call database cursor commit if you are coding on Odoo shell, it calls automatically though if you are doing it in a module. Just a friendly reminder!