콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
6 답글
19434 화면

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'),

아바타
취소
베스트 답변

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

베스트 답변

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

아바타
취소
베스트 답변

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

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

관련 게시물 답글 화면 활동
3
5월 25
1831
1
4월 25
1369
3
9월 24
14313
2
2월 24
2539
1
7월 23
2785