{"id":471,"date":"2019-08-29T19:44:43","date_gmt":"2019-08-29T19:44:43","guid":{"rendered":"https:\/\/mellowhost.com\/blog\/?p=471"},"modified":"2019-08-29T19:44:43","modified_gmt":"2019-08-29T19:44:43","slug":"how-to-hide-a-column-dynamically-in-tree-view-on-odoo","status":"publish","type":"post","link":"https:\/\/mellowhost.com\/blog\/how-to-hide-a-column-dynamically-in-tree-view-on-odoo.html","title":{"rendered":"How to hide a column &#8216;dynamically&#8217; in Tree View on Odoo"},"content":{"rendered":"\n<p>Odoo field has an attribute called &#8216;attrs&#8217;. This adds custom attribute to the Odoo form\/tree views. You may use different kind of attributes with attrs like &#8216;required&#8217;, &#8216;readonly&#8217; or &#8216;invisible&#8217;. For basic, to hide a field regardless of it be an individual entity or parent relation, we use the following:<\/p>\n\n\n\n<p><code>&lt;field name='field_name' invisible='1'\/><\/code><\/p>\n\n\n\n<p>This is useful when we are triggering a computed field that we don&#8217;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 &#8216;attrs&#8217; attribute on fields, for example:<\/p>\n\n\n\n<p><code>&lt;field name='product_id' attrs=\"{ 'invisible': [('is_set', '=', True)]}\"\/><\/code><\/p>\n\n\n\n<p>You might set the field <code>'is_set'<\/code> itself as invisible and a boolean computed field like the following:<\/p>\n\n\n\n<p><code>is_set = fields.Boolean(compute=\"_set_is_set\")<\/code><\/p>\n\n\n\n<p><code>def _set_is_set(self):<br>      if self.lot_id.life_date:<br>               self.is_set = True<br>      else:<br>                self.is_set = False<\/code><\/p>\n\n\n\n<p>Seems easy, right? But there is a catch, the above won&#8217;t hide the whole column if you are on a place like &#8216;purchase.order.line&#8217; or &#8216;stock.move.line&#8217;. How can you work on such cases? It only works on individual entry, but not on a relational column. There is an attribute called &#8216;column_invisible&#8217; same like &#8216;invisible&#8217; 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 &#8216;show_expiry&#8217; in a picking operation:<\/p>\n\n\n\n<p><code>&lt;field name=\"show_expiry\" attrs=\"{ 'column_invisible' : [('parent.picking_type_code', 'in', ['incoming'])]}\" \/><\/code><\/p>\n\n\n\n<p>Above code checks the &#8216;picking_type_code&#8217; value from &#8216;stock.picking&#8217; model, and hides the column if it&#8217;s an incoming shipment and shows when it&#8217;s an outgoing shipment. That means, the above, would show the column (show_expiry) if it&#8217;s a Delivery to customer location or you already have the product, but won&#8217;t show if you are doing a GRN, means you don&#8217;t have the stock yet, just arriving.<\/p>\n\n\n\n<p>Pretty simple, isn&#8217;t it? Good luck.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Odoo field has an attribute called &#8216;attrs&#8217;. This adds custom attribute to the Odoo form\/tree views. You may use different kind of attributes with attrs like &#8216;required&#8217;, &#8216;readonly&#8217; or &#8216;invisible&#8217;. For basic, to hide a field regardless of it be an individual entity or parent relation, we use the following: &lt;field name=&#8217;field_name&#8217; invisible=&#8217;1&#8217;\/> This is &hellip; <a href=\"https:\/\/mellowhost.com\/blog\/how-to-hide-a-column-dynamically-in-tree-view-on-odoo.html\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to hide a column &#8216;dynamically&#8217; 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],"tags":[],"_links":{"self":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/471"}],"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=471"}],"version-history":[{"count":1,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/posts\/471\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mellowhost.com\/blog\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}