{"id":468,"date":"2019-08-20T17:46:45","date_gmt":"2019-08-20T17:46:45","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=468"},"modified":"2019-08-20T17:46:45","modified_gmt":"2019-08-20T17:46:45","slug":"how-to-use-dynamic-domain-in-odoo-12-many2one-relation","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-use-dynamic-domain-in-odoo-12-many2one-relation.html","title":{"rendered":"How to use dynamic &#8216;domain&#8217; in Odoo 12 Many2one relation"},"content":{"rendered":"\n<p>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&#8217;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 &#8216;relational objects&#8217;. The only way left is to use a Many2one relational field. <\/p>\n\n\n\n<p>Now Many2one relational field now make it a bit complicated as, you can&#8217;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 &#8216;self&#8217;, and some from another relation. Here is how you can achieve this!<\/p>\n\n\n\n<p>I will create a simple model with two fields, one is Char and the other is Many2one:<\/p>\n\n\n\n<p><code>_name = 'my.product.manager'<br><br>sku = fields.Char(string='SKU', required=True)<br>basket = fields.Many2one('basket.location', string='Product Basket Location', required=True)<\/code><\/p>\n\n\n\n<p>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. <\/p>\n\n\n\n<p>First, we create a new Many2one relation field on &#8216;purchase.order.line&#8217; model in relation to &#8216;my.product.manager&#8217;<\/p>\n\n\n\n<p><code>_inherit = 'purchase.order.line'<br>basket_location_id = fields.Many2one( 'my.product.manager', 'Basket Location')<\/code><\/p>\n\n\n\n<p>Cool, now you don&#8217;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<\/p>\n\n\n\n<p><code>@api.onchange('product_id')<br>def onchange_basket(self):<br>     res = {<br>            'domain' : {<br>                       'basket_dest_id' : [('sku', '=', self.product_id.default_code)],<br>                }<br>        }<br>        return res<\/code><\/p>\n\n\n\n<p>Neat! Pretty straight forward, isn&#8217;t it? Odoo 12 doesn&#8217;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?<\/p>\n\n\n\n<p>Happy coding! <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-use-dynamic-domain-in-odoo-12-many2one-relation.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to use dynamic &#8216;domain&#8217; in Odoo 12 Many2one relation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[292,271],"tags":[],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/468"}],"collection":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":1,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":469,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/468\/revisions\/469"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}