콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
5504 화면

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 :)

아바타
취소
베스트 답변

'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

아바타
취소
관련 게시물 답글 화면 활동
0
9월 23
2073
0
9월 23
1816
2
6월 25
11297
6
10월 23
22146
3
3월 24
9562