Skip to Content
Menu
This question has been flagged
1 Reply
3222 Views

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
Discard
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
Discard
Related Posts Replies Views Activity
0
Apr 16
9
1
Apr 23
3490
2
Feb 21
10567
2
Aug 15
3639
0
Mar 15
4000