Skip to Content
Menu
This question has been flagged

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
parentname_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?

Avatar
Discard

When should you use name_search method: https://goo.gl/7PHhPP

Author Best Answer

I have solved it by including all children in the name_search method:


The method now looks like:

 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) if len(locs) == 1: child_dom = [('parent_left', '>', locs[0].parent_left), ('parent_left', '<', locs[0].parent_right)] child_locs = self.search(child_dom) locs = locs + child_locs return locs.name_get()

Avatar
Discard
Best Answer

Dear Habib,

You may try to check by altering the domain as display_name ilike name.

Or as a better option, use fnct_search for your display_name field. Examples are available in official addons.

Thanks.

Avatar
Discard
Related Posts Replies Views Activity
0
Jul 17
3766
1
Jul 15
5862
name_get Solved
2
Jul 24
3998
1
Jan 24
296
1
Dec 21
3406