This question has been flagged
6 Replies
16606 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'),

Avatar
Discard
Best Answer

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



Avatar
Discard
Author

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

Best Answer

I made a video with explanation https://youtu.be/y82IftkWdCM

Avatar
Discard
Best Answer

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;

}));

}

}


Avatar
Discard
Author

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 :(

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