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

I want to create a new register, but I need to put the value of a parent field as a default value of a child field.
I know that you can send parent values through context from XML and whenever a new form is opened, "default_get" of that object will be executed.

Example, [In XML view]:
<field name="one2many_ids" context="{'variable1' : name, 'variable2' : active_id, 'default_column1' : 'samplevalue'}">
Note: [There are 3 ways of setting default value]
 1. Variable1: you can pass parent value, which you can retrieve in default_get method.. 2. Variable2: "active_id" is a keyvalue, which provides ID of its immediate parent object, so in default_get you can browse and fetch the parent record , provided parent record must be saved in order to get it... 3. default_column: straight away setting default values for a column.

My problem now is:
How can I access to those values in the default_get method?

I have tried this:

@api.model
def default_get(self, vals):
    context = dict(self.env.context)
    variable1 = context.get('variable1', False)
    print ' variable1'

But this has not worked.

아바타
취소
베스트 답변

Hi,


You don't need to use default_get.

In your context in xml in the one2manyfield :
context = {'parent_id': active_id, 'parent_model': 'your.parent_model'}

If you want to get the parent obj in the child object to set some default fields of the child, you could try smth like this in the child class (replace with your values, of course) :

def _get_some_default(self):

     parent_id = self.env.context.get('parent_id') 

     parent_model = self.env.context.get('parent_model')       

     if parent_id and parent_model:

         parent_obj = self.env[parent_model].browse(parent_id)

         # now you have the parent obj to do what you want

         default_value = ... use the parent

         return default_value


some_field = fields.Char(default=_get_some_default)



아바타
취소
작성자

Ok, I pass this value in the context, but when do I pick it? In what method?

Why do you need to pick it?
It should be set by default when you open the form by clicking on the field.

Le jeu. 7 juin 2018 18:22, Nacho <nacho@baintex.com> a écrit :

Ok, I pass this value in the context, but when do I pick it? In what method?

--
Nacho

Sent by Odoo S.A. using Odoo.

작성자

But I need to pick the value to access to some fields of the parent and so stuff for the default value of some field of the child.

I edited my answer

작성자

Okay, I have added a print after active_id and active_model and both values are None.

If I print the context, I have only this:

context= {u'lang': u'es_ES', u'tz': u'Europe/Madrid', u'uid': 1}

Can you try passing them from the xml ?

In your context in xml in the one2manyfield :
context = {'parent_id': active_id, 'parent_model': 'your.parent_model'}

=> and use this in the default function instead of active_id/active_model

2018-06-08 11:01 GMT+02:00 Nacho <nacho@baintex.com>:

Okay, I have added a print after active_id and active_model and both values are None.

If I print the context, I have only this:

context= {u'lang': u'es_ES', u'tz': u'Europe/Madrid', u'uid': 1}

--
Nacho

Sent by Odoo S.A. using Odoo.




--
Adrien Combourieu
00 33 6 29 85 94 59
adrien.combourieu@gmail.com
작성자

That worked :)

I realised that I was passing the context through a child field, and that is wrong. The correct way is to pass the context through the One2many field.

OK I edited the answer. Can you upvote it ?

Get parent form value in one2many form view or pop up form view, refer below link hope this may helps you

http://learnopenerp.blogspot.com/2018/01/get-parent-form-value-in-one2many-form.html

작성자

Thank you both.

@Adrien I don't know why I cannot vote up your answer. I tried with different browser, and it seems that its a bug with the web. But I have voted it as a solution.

관련 게시물 답글 화면 활동
0
10월 23
3049
4
12월 21
33359
2
12월 20
9147
2
8월 18
7672
1
7월 17
4680