Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How to prefill One2many -> Many2one ?

Sven Rißmann
on 4/18/16, 11:40 AM 272 views


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)


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.

On 4/19/16, 5:22 AM

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

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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 4/18/16, 11:40 AM
Seen: 272 times
Last updated: 4/19/16, 5:22 AM