I need to hide a column depending on a condition. This is a snippet of the Shipment Package Line model:
class ShipmentPackageLine(models.Model):
_name = 'shipment.package.line'
_rec_name = 'package'
...
gross_column_air_hidden = fields.Boolean(compute='_compute_column_visibility')
@api.depends('shipment_id.transport')
def _compute_column_visibility(self):
for line in self:
if (line.shipment_id.transport == 'air'):
line.gross_column_air_hidden = True
_logger.info('Gross column set to invisible')
else:
line.gross_column_air_hidden = False
_logger.info('Gross column set to visible')
Here is the Freight Shipment model:
class FreightShipment(models.Model):
_name = 'freight.shipment'
_inherit = ['mail.thread', 'mail.activity.mixin']
...
transport = fields.Selection(([('air', 'Air'), ('ocean', 'Ocean'), ('land', 'Land')]),
string='Transport Via')
freight_packages = fields.One2many('shipment.package.line', 'shipment_id')
This is the XML view template snippet, please note that the model used in the Freight Shipment
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record model="ir.ui.view" id="freight_shipment_form_view">
<field name="name">freight.shipment.form.view</field>
<field name="model">freight.shipment</field>
<field name="arch" type="xml">
<form edit="stage_id != 5">
...
<notebook>
...
<page string="Package Details">
<field name="freight_packages" nolabel="1" widget="one2many_list">
<tree string="Package">
<field name="gross_column_air_hidden" invisible="1" column_invisible="1"/>
<field name="name" />
<field name="seal_number"/>
<field name="package_type"/>
<field name="package" required="1"/>
<field name="container_type" optional="hide"/>
<field name="qty" sum="Total Qty."/>
<field name="volume" column_invisible="gross_column_air_hidden"/>
<field name="gross_weight" column_invisible="gross_column_air_hidden"/>
<field name="net_weight" sum="Total Net"/>
<field name="total_cbm" column_invisible="gross_column_air_hidden"/>
<field name="line_added" column_invisible="1"/>
<button name="action_insert_line_service" type="object" string="Add to Service"
icon="fa-plus-square-o" invisible="line_added"/>
</tree>
...
I got the error:
UncaughtPromiseError > OwlError
Uncaught Promise > An error occurred in the owl lifecycle (see this Error's "cause" property)
OwlError: An error occurred in the owl lifecycle (see this Error's "cause" property)
Error: An error occurred in the owl lifecycle (see this Error's "cause" property)
at handleError (.../web/assets/518164d/web.assets_web.min.js:916:101)
at App.handleError (.../web/assets/518164d/web.assets_web.min.js:1559:29)
at Fiber._render (.../web/assets/518164d/web.assets_web.min.js:941 :19)
at Fiber.render (.../web/assets/518164d/web.assets_web.min.js:939:6)
at ComponentNode. initiateRender (.../web.assets_web.min.js:1009:47)
Caused by: EvalError: Can not evaluate python expression: (bool(gross_column_air_hidden))
Error: Name 'gross_column_air_hidden' is not defined
EvalError: Can not evaluate python expression : (bool(gross_column_air_hidden))
Error: Name 'gross_column_air_hidden' is not defined
at evaluateExpr (.../web/assets/518164d/web.assets_web.min.js:3068:54)
at evaluateBooleanExpr (.../web/assets/518164d/web.assets_web.min.js:3071:8)
at Object .evalViewModifier (.../web/assets/518164d/web.assets_web.min.js:8599:154)
at ListRenderer.evalColumnInvisible (.../web/assets/518164d/web.assets_web.min.js:9508:56)
at .../web/assets/518164d/web.assets_web.min.js:9409:9
at Array .filter (<anonymous>)
at ListRenderer.getActiveColumns (.../web/assets/518164d/web.assets_web.min.js:9407:47)
at ListRenderer.setup (.../web/assets/518164d/web.assets_web.min.js:9401:456)
at ListRenderer.setup (.../web/assets/518164d/web.assets_web.min.js:17112:861)
at ListRenderer.setup (.../web/assets/518164d/web.assets_web.min.js:17793:535)
If i used invisible instead of column_invisible, there would be no error, but the column will be shown
How can I fix this?
I noticed that when I used 'invisible' instead of 'column_invisible', the values under the columns are hidden/not showing but the column is still showing. If I used 'column_invisible', it will prompt an error that the 'gross_column_air_hidden' is undefined even though I have defined it.
Hi Sahar, I cannot comment on your reply since I do not have points. How should I solve this? If I use a field inside the tree from the shipment.package, it would not be recognize. For example, the transport field, it will show Caused by: EvalError: Can not evaluate python expression: (bool(transport == 'air'))
Error: Name 'transport' is not defined
EvalError: Can not evaluate python expression: (bool(transport == 'air'))