Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Howto change the label of a field by python code (on_change method)?

By
Gregor Heger
on 3/15/14, 10:42 PM 3,749 views

I would like to change the label of a field called "target_field", if the selection field contract_type changed the value to sale. This belongs to the view of the contract_type:

<field name="contract_type" on_change="on_change_contract_type(contract_type)"/>
<field name="target_field">

And here the corresponding on_change method.

def on_change_contract_type(self, cr, uid, ids, contract_type, context={}):
    res={}
    if contract_type == 'sale':
        res['target_field']['label'] = 'Sales Contract'
    return {'value': res}

I know it doesn't work, because ['label'] does not excist. But is there any way, to set it by code? An other option which is working is to play with attrs und invisible attributes directly in the xml file, but it is a bad coding design approach in my opinion.

I just bumped into the same issue. I will keep an eye on this post, or if I find the solution I will return with it.

Adrian
on 3/18/14, 12:09 PM
3

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 3/19/14, 6:18 AM

Another way would be using two label fields:

<field name="contract_type"/>
<label for="target_field" attrs="{'invisible': [('contract_type', '=', 'sale')]}"/>
<label string="Sales Contract" attrs="{'invisible': [('contract_type', '!=', 'sale')]}"/>
<field name="target_field" nolabel="1"/>

No onchange method is needed this way.

Regards.

An excelent solution. On the other hand, I needed to take the labels from a custom product details table. So, I needed the onchange event. But your solution is much more appropriate for Gregor's need.

Adrian
on 3/19/14, 7:18 AM
1
Adrian
On 3/19/14, 5:54 AM

I have found a solution that works for me.

  • I have created an additional field of type char where I will store the label
  • In the onchange method I am setting the value for this field
  • In the XML file you have to put nolabel="1" for both fields (label and target field) and the label field will also have readonly="1"

So, your XML file will look like this:

   <field name="contract_type" on_change="on_change_contract_type(contract_type)"/>
   <field name="label_field" readonly="1" nolabel="1" />
   <field name="target_field" nolabel="1" />

In this way, after the onchange event takes place, the label field will be displayed with the computed value and it will look like an actual label.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 3/15/14, 10:42 PM
Seen: 3749 times
Last updated: 3/16/15, 8:10 AM