Skip to Content
Menu
This question has been flagged

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?




Avatar
Discard

'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.

Best Answer

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

Avatar
Discard
Author

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?

Author

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.

Related Posts Replies Views Activity
1
Dec 19
4651
0
Oct 18
3011
1
Sep 19
3013
2
Sep 19
5637
3
Jul 18
2604