{"id":482,"date":"2019-12-16T09:09:06","date_gmt":"2019-12-16T09:09:06","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=482"},"modified":"2019-12-16T09:09:06","modified_gmt":"2019-12-16T09:09:06","slug":"how-to-hide-a-column-dynamically-in-a-parent-model-in-tree-view-on-odoo","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-hide-a-column-dynamically-in-a-parent-model-in-tree-view-on-odoo.html","title":{"rendered":"How to Hide a Column Dynamically in a Parent Model in Tree View on Odoo"},"content":{"rendered":"\n<p>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:<br><br><a href=\"https:\/\/mellowhost.com\/blog\/how-to-hide-a-column-dynamically-in-tree-view-on-odoo.html\">How to hide a column \u2018dynamically\u2019 in Tree View on Odoo<\/a><\/p>\n\n\n\n<p>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 &#8216;Deliver Orders&#8217; under Inventory Overview, if you want to hide a column based on condition, how do we do that?<\/p>\n\n\n\n<p>The answer is, we use data from context and set the attribute &#8216;invisible&#8217; 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. <\/p>\n\n\n\n<p>I will discuss on how can we use the existing context key:value pair in hiding a column on stock.picking model.<\/p>\n\n\n\n<p>First, let&#8217;s imagine, we have a character field on stock.picking called &#8216;woocommerce_id&#8217;.<\/p>\n\n\n\n<p><code>class stock_picking(models.Model):<br>          _inhert = 'stock.picking'<br>          <br>          woocommerce_id = fields.Char(string=\"Woocommerce ID\")<\/code><\/p>\n\n\n\n<p>Now the xml for Inventory overview stock.picking trees would be inheriting stock.vpicktree<\/p>\n\n\n\n<p> <code>&lt;record\u00a0id=\"inherit_delivery_picking\"\u00a0model=\"ir.ui.view\"> <br> &lt;field\u00a0name=\"name\">stock.picking.inherit.delivery.picking&lt;\/field> <br> &lt;field\u00a0name=\"model\">stock.picking&lt;\/field> <br> &lt;field\u00a0name=\"inherit_id\"\u00a0ref=\"stock.vpicktree\"\/> <br> &lt;field\u00a0name=\"arch\"\u00a0type=\"xml\"> <br> &lt;xpath\u00a0expr=\"\/\/field[@name='location_dest_id']\"\u00a0position=\"after\"> <br>&lt;field\u00a0name=\"woocommerce_id\"\/> <br>&lt;\/xpath><br>&lt;\/field><br>&lt;\/record><\/code><\/p>\n\n\n\n<p>Now, the above will show woocommerce_id in all vpicktree view, like &#8216;Delivery Orders&#8217;, &#8216;Receipts&#8217;, &#8216;Internal Transfers&#8217;. But What if, you want to show this field to only Delivery Orders?<\/p>\n\n\n\n<p>To do that, we need to know the value for &#8216;default_picking_type_id&#8217;. 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(&#8216;key_name&#8217;) to find the value for it).<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<p><code>&lt;field name=\"woocommerce_id\" invisible=\"context.get('default_picking_type_id') != 1\"\/><\/code><\/p>\n\n\n\n<p>Save it, upgrade the module, and see the magic! cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u2018dynamically\u2019 in Tree View on Odoo Above case works when you are trying a hide a column in a sale order line, &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-hide-a-column-dynamically-in-a-parent-model-in-tree-view-on-odoo.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Hide a Column Dynamically in a Parent Model in Tree View on Odoo&#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,293],"tags":[303,297,301,296,302],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/482"}],"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=482"}],"version-history":[{"count":1,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":483,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/482\/revisions\/483"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}