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

I have 3 classes:

class Parent(models.Model):

    _name='parent.parent'


class Child1(models.Model):

    _name="child.child1"

    _inherits={'parents.parents': 'parent_id'}

    parent_id = fields.Many2one('parent.parent', required=True, ondelete="cascade")

class Child2(models.Model):

    _name="child.child2"

    _inherits={'parents.parents': 'parent_id'}

    parent_id = fields.Many2one('parent.parent', required=True, ondelete="cascade")

How can I navigate from parent.parent to his (all) children? Do I need to declare to new fields in the parent like ?:

child1 = fields.Many2one('child.child1', store=False)

child2 = fields.Many2one('child.child2', store=False)

Is there a better solution?




아바타
취소

'store' is used for computed fields which are by default not stored in database, so you can add store=True to store it.

In your example, the use of store=False is irrelevant.

Also, it seems you need to understand better the inheritance concept and relational field concept. See my answer below.

베스트 답변

Hi,

1. About Model inheritance

First of all, are you sure you really need "_inherits" ?
It is much more common to use "inherit" which works like this :
_name = 'child1.child1'
_inherit = ['parent.parent']
And then you can use the field of the parent in the child object :
child1.parent_field

If you don't know the difference, see e.g. :

https://www.odoo.com/fr_FR/forum/how-to/developers-13/the-different-openerp-model-inheritance-mechanisms-what-s-the-difference-between-them-and-when-should-they-be-used-46


2. About parent / child relation :

This is not related with inheritance but to relational fields.

In that case you can have :

- a One2many field in the parent model to its children

example : child1_ids = fields.One2many(comodel_name='child1.child1', inverse_name='parent_id')

then you can do:

for child1 in parent.child1_ids:

    ... do smth ...

- a Many2one field in the child object to its parent (that must then be called 'parent_id' to correspond to the 'inverse_name' used before)

see for example the link between a partner and its contacts in native Odoo.

And explanation of relational fields :

https://www.odoo.com/fr_FR/forum/aide-1/question/relational-fields-114056

아바타
취소
작성자

Thanks, Yes I think I need inherits. Because I have many classes which use the parent.parent. If I use One2many and Many2one, I need to declare many of them in parent.parent. I want to access some attributes of the child1, child2, childn from parent.parent.

Maybe, I want to display/choose children with fields.Many2one(parent.parent) in a view.

Is this possible?

It is not clear what you mean and need.

Can you clarify what you need :

- parent/child relation between RECORDS ? ===> This is handled with relational fields

- or parent/child relation between MODELS ==> This is handled with inheritance

Maybe you could provide a minimal practical example of what you want to do?

작성자

class Selector(models.Model):

_name = 'selector.model'

selectOne = fields.Many2one('parent.parent')

fieldOfChildren1 = fields.Char(related='selectOne.attributeOfChildren1')

fieldOfChildren2 = fields.Integer(related='selectOne.attributeOfChildren2')

Is this somehow possible?

I think it is relation between MODELS.

관련 게시물 답글 화면 활동
1
12월 19
5936
0
10월 18
3967
1
9월 19
4214
2
9월 19
6646
3
7월 18
4039