I have a recursive model
class Location_Tags(models.Model): _name = "property.location.tag" _description = "Location Tag" name = fields.Char(string="Name", required=True, index=True) display_name = fields.Char(compute="_get_display_name") parent_id = fields.Many2one(property.location.tag', string="Parent Location") child_ids = fields.One2many(property.location.tag', parent_id', string="Children") property_ids = fields.Many2many(property.property', relation='property_location_tags', column1='tag_id', column2='property_id') _parent_store = True parent_left = fields.Integer('Left parent', select=True) parent_right = fields.Integer('Right parent', select=True) @api.one @api.depends('parent_id', 'name') def _get_display_name(self): self.display_name = self.name_get()[0][1] @api.multi def name_get(self): res = [] for record in self: names = [] current = record while current: names.append(current.name) current = current.parent_id res.append((record.id, ' / '.join(reversed(names)))) return res @api.model def name_search(self, name, args=None, operator='ilike', limit=100): args = args or [] if name: # Be sure name_search is symetric to name_get name = name.split(' / ')[-1] args = [('name', operator, name)] + args locs = self.search(args, limit=limit) return locs.name_get()
I want to be able to search the parent a higher level parent in the Many2one widget.
Assuming I have 3 Location Tags namely:
name | parent | name_get |
A | A | |
B | A | A / B |
C | B | A / B / C |
When I search for B in the parent_id field (Many2one widget) .. The results should be:
A / B
A / B / C
but instead I get only:
A / B
How would I do this?
I have tried storing the display_name with
store=True
and name_search uses
[('display_name', operator, name)]
but this is flawed because child display_names don't update when a parent name is changed.
Any ideas?
When should you use name_search method: https://goo.gl/7PHhPP