Skip to Content
Menú
This question has been flagged
1 Respondre
4354 Vistes

Hi,

I have two models connected via One2Many->Many2One relation. I create Model B from a tree-list in Model A.

Here is my code:

class dummy_workcenter(models.Model):

_inherit = 'mrp.workcenter'

machinetype = fields.Selection(string="Maschinenart", required=True, selection=[('a', 'Type A'), ('b', 'Type B')])

attributes = fields.One2many('dummy.workcenter.attribute', 'workcenter_id', string="Eigenschaften", required=True)


class dummy_workcenter_attribute(models.Model):

_name = 'dummy.workcenter.attribute'

name = fields.Char(string="Bezeichnung", required=True)

workcenter_id = fields.Many2one('mrp.workcenter', string="Maschine")

attributetype = fields.Char(compute='_attributetype')

attribute = fields.Selection(string="Type", selection='_attributeselection', required=True)


@api.depends('attributetype')

def _attributetype(self):

self.attributetype = self.workcenter_id.machinetype

def _attributeselection(self):

return [('1', 'Option 1'), ('2', 'Option 2')]

The computed Function "_attributetype" works fine, if model B is created and model A is saved.

But I want to have "attributetype" already in the "creation overlay" of model B.

So the question is, how can I use model A's values on model B's creation?!

(And if that works anyway, how can I use them if model A isn't saved already - in draft state!?!?!?)

... I hope I've explained it clear enough.

Avatar
Descartar
Best Answer

You can't use model A's values on B if A is not created.

To use A's values on B's creation you can use the onchange method

@api.onchange('workcenter_id')
def onchange_workcenter(self):
     self.attributetype = self.workcenter_id.machinetype

 Note: If you don't have calculations to do you can define the attributetype field as a relational field.

Avatar
Descartar
Related Posts Respostes Vistes Activitat
0
d’abr. 16
9
1
d’abr. 23
5694
2
de febr. 21
12707
2
d’ag. 15
4896
0
de març 15
4872