This question has been flagged
1 Reply
4456 Views

I'm creating a custom module in odoo and I'm struggling with an inheritance issue, let's say i have the following implementation :

class SuperModel(models.Model) :
   _name="model_name"
   _inherits={'model_name.one':'model_name_one_id',
              'model_name.two':'model_name_two_id'}
   selection = fields.Selection(selection=[('m1','Model one'),('m2','Model Two')])
   model_name_one_id = fields.Many2one(comodel_name="model_name.one",ondelete="cascade")
   model_name_two_id = fields.Many2one(comodel_name="model_name.two",ondelete="cascade")

class ModelOne(models.Model):
   _name="model_name.one"
   value_one = fields.Char()

class ModelTwo(models.Model):
   _name="model_name.two"
   value_two = fields.Char()

What i want to achieve, is by selecting "Model 1" or "Model 2" in the main model view, only the corresponding fields will be displayed and stored in the database.

But whenever i create a record for "SuperModel" both records are created in "ModelOne" and "ModelTwo" tables.

For example if i select "Model 1" and fill "value_one", when saving, an empty record is created in "Model 2" table (model_name_two_id == False). How can i prevent that ?

Thank you for helping :)

Avatar
Discard
Best Answer

'Inherits' works exactly in the way you described. Each time you create a child (SuperModel), the parents would be created (ModelOne, ModelTwo). E.g. when you create a product variant, a template must be created (see the 'product' module for examples).

Thus, in your case you should:

  1. Whether you really need this inheritance at all. According to your example, mere many2one fields would be enough

  2. Perhaps 'inherits' should be the opposite, shouldn't it? One & Two should inherit data of Super? Then, they would not be created?

  3. Avoiding inheritance to store some values of 2 parent models, use field attributes 'related'. See the section 'Related fields' here: https://www.odoo.com/documentation/8.0/reference/orm.html

Avatar
Discard