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

@api.onchange('product_id')
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:

sale_order.order_line

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:

sale_order_new.write({
'order_line': [
(0,0, {
'order_id': sale_order.id,
'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
sale_order_line.create(order_line_dict)

# print sale order lines
sale_order_line

Note

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

self.env.cr.commit()

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!