I would like to create a field that has several categories of professions (parent elements) and at the end the profession corresponding to that category appears, as in the product category field of the product model.
How can I do this?
I would like to create a field that has several categories of professions (parent elements) and at the end the profession corresponding to that category appears, as in the product category field of the product model.
How can I do this?
You can define a computed field that shows the parent categories' names and set it as the _rec_name to let Odoo use it as a display name in the Many2one fields.
Example:
class ProfessionCategory(models.Model):
_name = "profession.category"
_description = "Profession Category"
_rec_name = 'complete_name'
name = fields.Char('Name', index=True, required=True)
complete_name = fields.Char(
'Complete Name', compute='_compute_complete_name', recursive=True,
store=True)
parent_id = fields.Many2one('profession.category', 'Parent Category', index=True, ondelete='cascade')
@api.depends('name', 'parent_id.complete_name')
def _compute_complete_name(self):
for category in self:
if category.parent_id:
category.complete_name = '%s / %s' % (category.parent_id.complete_name, category.name)
else:
category.complete_name = category.name