Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to search parent_id name in Many2one widget

By
Habib
on 9/1/15, 8:44 PM 851 views

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?

1
Habib
On 9/2/15, 5:07 AM

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()

0

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 9/2/15, 3:20 AM

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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 9/1/15, 8:44 PM
Seen: 851 times
Last updated: 9/4/15, 8:46 AM