Odoo Help

1

Fields definitions parameter change_default?

By
Zbik
on 2/12/16, 8:32 AM 1,838 views

I have not found any documentation on the parameter change_default. Can anyone knows its meaning?

Example from sale.py:

'product_id': fields.many2one('product.product', 'Product', domain=[('sale_ok', '=', True)], change_default=True, readonly=True, states={'draft': [('readonly', False)]}, ondelete='restrict'),

3

Hi,


A example will maybe help you...

Take a model with a field A, and a field B.  

The field B has change_default set to True.


Now, if you go to the form view in debug mode and set a default value for the field A. You can set this value depending on the value of B...



Thank you very much for this helpful picture and answer. Now I understand :)

Zbik
on 2/14/16, 3:30 AM
1
Axel Mendoza
On 2/12/16, 11:21 AM

Well I haven't see the answer of @jke before find mine and I think that this question haven't had an answer. That's a way to use it. I will write my own version that could help developers more and also is more technical to express value change rules by xml ir.values record. I don't mean that @jke answer is incorrect. Just that these are the technical insides.

The other way is by writing ir.values records with type='default' and key2='field_with_change_default=value' for the model in question. It will cause that the name and value of those ir.values records that match the key2 value condition will be used as a result of the change of the field_with_change_default. As example you could use the field zip of the res.partner model that has defined with change_default=True. Writing some records like this:

<record model="ir.values" id="partner_zip_name">
<field name="name">name</field>
<field name="value">Axel</field>
<field name="key">default</field>
<field name="key2">zip=5000</field>
<field name="model">res.partner</field>
</record>
<record model="ir.values" id="partner_zip_phone">
<field name="name">phone</field>
<field name="value">+99999999</field>
<field name="key">default</field>
<field name="key2">zip=5000</field>
<field name="model">res.partner</field>
</record>

Will cause that when the zip code in a partner is set to 5000 the name will be changed to 'Axel' and the phone will be changed to '+99999999' like if you have an onchange to return those values and also this could rewrite an existing value returned by an onchange execution that get executed before the code responsible for this, runs. Here is the js code that allow this behavior for references. 

if (field && field.change_default) {

var value_;

if (response.value && (fieldname in response.value)) {

// Use value from onchange if onchange executed

value_ = response.value[fieldname];

} else {

// otherwise get form value for field

value_ = self.fields[fieldname].get_value();

}

var condition = fieldname + '=' + value_;

if (value_) {

defs.push(self.alive(new instance.web.Model('ir.values').call(

'get_defaults', [self.model, condition]

)).then(function (results) {

if (!results.length) {

return response;

}

if (!response.value) {

response.value = {};

}

for(var i=0; i<results.length; ++i) {

// [whatever, key, value]

var triplet = results[i];

response.value[triplet[1]] = triplet[2];

}

return response;

}));

}

}


It is actual in new api? My api@onchange are tirggered without this parameters (with change_default = False)? In old api I did never use this parameter too, but on_change works? I think I do not understand :(

Zbik
on 2/12/16, 11:36 AM

I already know how that works, very tricky, but works. let me rewrite my answer on that with an example of how to test it

Axel Mendoza
on 3/17/16, 1:11 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 2/12/16, 8:32 AM
Seen: 1838 times
Last updated: 1/26/17, 2:24 PM