I am uncertain regarding what child_of
does after reading the docs and the use of child_of
operator over using in
operator for filtering
Odoo v10 domain docs can be found here which states the following for child_of
is a child (descendant) of a value record.
Takes the semantics of the model into account (i.e following the relationship field named by _parent_name)
Consider the model product.product
having a many2one field called pos_categ_id
that points to model pos.category
To only get products with PoS category id of x
, domain value of ['pos_categ_id', 'child_of', x]
can be used, however ['pos_categ_id', 'in', [x]]
seems to also do the same thing.
In addition to that, in
operator can be used for many2many and one2many fields with same format as above example while use of child_of
operator for those two field types cause errors.
Example the domain ['session_ids', 'child_of', [572]]
where session_ids
is a many2many field causes the following error Invalid field 'parent_id' in leaf \"<osv.ExtendedLeaf: ('parent_id', 'in', [572]) on pos_session (ctx: )>\"
So, what is the example case where child_of
is preferred over in
operator? Both seems to function the same except in
can work on all types of relational fields. Second, what does the second line of docs for child_of
mean by the phrase following the relationship field named by _parent_name
?
No they are not the same, yes they are the same in the simple example that you used:
To only get products with PoS category id of x, domain value of ['pos_categ_id', 'child_of', x] can be used, however ['pos_categ_id', 'in', [x]] seems to also do the same thing.
But consider this case:
# x
# / \
# f b
# / \
# c d
when we do this ['pos_categ_id', 'child_of', x]
this will return product that their category is one of this list [x, f, b, c, d]
it's equivalent to ['pos_categ_id', 'in', [x, f, b, c, d]]
.
because they are all child of x (child_of
includes x itself).
but this: ['pos_categ_id', 'in', [x]]
will return only product that their category is X
but will not return product with category b or c ..etc
.
To activate this feature in your custom model you should set this attribute
class YourModel(models.Model):
_name = 'some.model'
_parent_name = 'parent_id' # by default its name is parent_id you can change it
_parent_store = True # tell odoo that this model support parent & child relation ship
# same name of _parent_name
parent_id = fields.Many2one('some.model', 'Parent')
Basically child_of
functionality is provided by odoo, not python.
But to understand it you must need to knowledge of parent
, 'child` relationship in database concept.
If any of your tables are using a self join like below example.
_name = 'my.table'
parent_id = fields.Many2one('my.table', string='Parent Id')
In the above case, your model has connected itself. In this type of cases in Odoo, you can use child_of
operator in the domain.
So, it will search in database like parent_id = <your_value>
.
For in
domain,
You can pass the list of data which you need to search in id
field. It similar to any database in
operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With