Model is product.category
, want to inherit and add a field level
to add the levels to find which level record I want.
We knew that by default (as recommended) that first entry *All*
here should present to start the parent child relations.
When the user creates a new category and select All as its parent, we knew that its level is 1, and we can add 2, programmatically it should be parent_level + 1 ... so we can add levels of any depth.
I don't know how to code it, so please help me for the below onchange function (or any other better approach, please specify), guide/modify it to achieve what I want to have.
@api.onchange(parent_id):
def _onchange_parent_id(self):
find the level of parent here...
self.level = parent_level + 1
not sure if i understand your question correctly, but if you just want a level to your category and provided the parent field name is parent_id:
level = fields.Integer('Level', compute='_compute_level')
@api.depends('parent_id')
def _compute_level(self):
for record in self:
record.level = (record.parent_id.level or 0) + 1