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 read 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.
My questions are:
I have tried this:
In XML view:
<field name="my_child_field" context="{'variable1': parent}"/>
In .py file:
@api.model
def default_get(self, vals):
context = dict(self.env.context)
variable1 = context.get('variable1', False)
print ' variable1'
That prints False as variable1 value.
Let me assume you have the following constellation:
class MyParent(models.Model):
_name = "my.parent"
child_ids = fields.One2many(
comodel_name="my.child", inverse_name="parent_id")
my_default_for_child = fields.Char()
class MyChild(models.Model):
_name = "my.child"
parent_id = fields.Many2one(comodel_name="my.parent")
my_field_for_default = fields.Char()
And of course a view definition. Just put the default value in the context, no need to use or extend default_get()
:
<record id="my_parent_form_view" model="my.parent">
<field name="name">my.parent.form.view</field>
<field name="model">my.parent</field>
<field name="arch" type="xml">
<form>
<field name="my_default_for_child" />
<field name="child_ids"
context="{'default_my_field_for_default': my_default_for_child}">
<tree editable="bottom">
<field name="parent_id" invisible="1" />
<field name="my_field_for_default" />
</tree>
</field>
</form>
</field>
</record>