Hi, i’m customizing sale model to allow users to have also a reference field on sale_lines. I’m doing the same way as the field ‘delivery_date’ in purchase model.
class SaleLine(metaclass=PoolMeta):
__name__ = 'sale.line'
reference = fields.Function(fields.Char('Reference',
states={
'invisible': ((Eval('type') != 'line')
| Eval('reference_edit', False)),
},
depends=['type', 'reference_edit']),
'on_change_with_reference')
reference_edit = fields.Boolean(
"Edit Reference",
states={
'invisible': Eval('type') != 'line',
'readonly': Eval('sale_state').in_([
'done', 'cancelled']),
},
depends=['type', 'sale_state'],
help="Check to edit the reference.")
reference_store = fields.Char(
"Reference",
states={
'invisible': ((Eval('type') != 'line')
| ~Eval('reference_edit', False)),
'readonly': Eval('sale_state').in_([
'done', 'cancelled']),
},
depends=['type', 'reference_edit', 'sale_state'])
@classmethod
def default_reference_edit(cls):
return False
@fields.depends('sale', '_parent_sale.reference', 'reference_edit',
'reference_store')
def on_change_with_reference(self, name=None):
if self.reference_edit and self.reference_store:
return self.reference_store
if self.sale:
return self.sale.reference
So users can have a reference by default which is sale_reference in function field ‘reference’, but they could also having a different reference on the sale_line by checking ‘reference_edit’ and setting a value in field ‘reference_store’
A switch is done in the view form to display the correct field (depending if ‘reference_edit’ is checked or not):
sale_line_form.xml
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<data>
<xpath expr="/form/notebook/page[@id='general']/field[@name='shipping_date']" position="after">
<label id="reference" string="Reference:"/>
<group id="reference" col="-1">
<field name="reference" xexpand="0"/>
<field name="reference_store" xexpand="0"/>
<field name="reference_edit" xexpand="0"/>
</group>
</xpath>
</data>
Until now everything is working well…
But i want also display that field ‘reference’ in the tree view…
sale_line_tree.xml
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<data>
<xpath expr="/tree/field[@name='amount']" position="after">
<field name="reference"/>
</xpath>
</data>
My function field ‘reference’ can display the correct value depending if reference_edit is checked or not.
But as his state is ‘invisible’ when reference_edit is checked, the value of ‘reference’ field is not displayed in the tree view…
So, my question:
- Is it possible to have the state ‘invisible’ only in form view ?
- Or should i create another function field ‘reference_display’ (with the same code as ‘reference’ but without state condition to display this field correctly in the tree view ?
- Another idea ?
Thanks!