Use 'colspan' in view_attributes to remove a field

I’m struggling with the use of the view_attributes. I want to remove a field from a form when a certain condition is met. Otherwise the field should be visible.

In my XML I have:

<?xml version="1.0"?>
<form col="4">
  ......
  <label name="order"/> # Many2One
  <field name="order"/>
  <label name="location"/> # Selection field
  <field name="location"/>
  ......

Now I want to remove the field location from the form when order is empty. I tried:

@classmethod
def view_attributes(cls):
    return super(Order, cls).view_attributes() + [
    ('/form/field[@name="location"]', 'colspan',
            If(Bool(Eval('order', -1)), -1, 2),
    )]

Basically I want to say “If order is empty, set the colspan of the location to -1. Else do nothing”. I have tried several versions, but none of them are working. On the client side I get a message like:

ValueError: invalid literal for int() with base 10: '{"__class__": "If", "c": {"__class__": "Bool", "v": {"__class__": "Eval", "v": "order", "d": -1}}, "t": -1, "e": 2}'

I don’t want to make the field just invisible because I want to remove the empty space as well.

How can I achieve this?

You can not. colspan can not be dynamic, it is set only once when the view XML is parsed.
If you want to hide a field based on a statement, you should use the states['invisible'] of the field.

But states['invisible'] will only hide the field and does not give the space back right?
Would it be possible to display another field in that location? or will it be left empty?

The views are based on a grid system.

You can use a group to put multiple tag under the same cell.

But I think you should embrace the Tryton design and not try to simulate another.

For completeness there is this proposal Issue 7492: Support colspan -1 & 0 - Tryton issue tracker but this will not make them dynamic.

Thanks, I read that one and came up with the idea to use the colspan in the view_attributes.

For even more completeness, I had to hide two fields which makes it one row in the grid. I did put that row just above the tabs. Using it this way and hiding the fields, removes the empty space. So no need to use view_attributes, this can be defined on the field itself.

<?xml version="1.0"?>
<form col="4">
  ......
  <label name="order"/> # Many2One
  <field name="order"/>
  <newline/>
  <label name="location"/> # Selection field
  <field name="location"/>
  <label name="user"/> # Selection field
  <field name="user"/>
  <notebook colspan="4">
    <page string="General" id="general">
      ........  # page stuff
    </page>
    ........ # do more paged stuff
  </notebook>
  ......

So when setting the state to invisible for location and user the notebook will consume that empty space.