This question has been flagged
1 Reply
25557 Views

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.

Avatar
Discard
Best Answer

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)



Avatar
Discard
Author

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.

Author

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

Author

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
Author

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

Author

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.