I'am facing this problem when I'am trying to get/search name of partner using Many2one fields in my custom models. I'am trying to compare with the declaration of partner_id in sale/Quotation module, but I didn't find any difference, Btw When I'am trying search name of customer in quotation this error doesn't display.
Declaration field :*.py
partner_id = fields.Many2one('res.partner', string="Client", required=True)
Declaration field :*.xml
<group name="partner_info" string="Info client">
<field name="partner_id" />
<label for="street" string="Address"/>
<div class="o_address_format">
<field name="street" placeholder="Street..." class="o_address_street" />
<field name="city" placeholder="City" class="o_address_city" />
<field name="state_id" class="o_address_state" placeholder="State" options="{"no_open": True}"/>
<field name="zip" placeholder="ZIP" class="o_address_zip" />
</div>
<label for="street" string="Contact"/>
<div name="contact">
<field name="phone" widget="phone" placeholder="Tél ° "/>
<field name="email" widget="email" placeholder="E-mail"/>
</div>
</group>
After deep searching, I could find the source of problem, when we put a letter in the partner fields, It will automatically call the function name_search in base module, witch search in res_partner table using SQL query :
select id
from res_partner
where 'entered value' [ '=' or 'like' or 'ilike' or '=like' or
'=ilike'] [email, or name, ref ]...`;
This query return a list of tuples
[(id1, 'partner name1'), (id2, 'partner name2')]
and the function return the second elm from every tuple witch is the name of partner, but if you have some partner with name field is empty the error will displayed, and this explains why some letters don't generate the error message. Some letters don't generate the error message because the SQL query search based on multiple fields as Email, reference, name of partner.
And the existence of this function just in partner, and not in every table explains that the main objective of the function is to allow us to search based on other fields [email, reference ...]
The path of function : /addons/base/res/res_partner.py
line 640
@api.model
def name_search(self, name, args=None, operator='ilike', limit=100):
if args is None:
args = []
if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'):
self.check_access_rights('read')
where_query = self._where_calc(args)
self._apply_ir_rules(where_query, 'read')
from_clause, where_clause, where_clause_params = where_query.get_sql()
where_str = where_clause and (" WHERE %s AND " % where_clause) or ' WHERE '
# search on the name of the contacts and of its company
search_name = name
if operator in ('ilike', 'like'):
search_name = '%%%s%%' % name
if operator in ('=ilike', '=like'):
operator = operator[1:]
unaccent = get_unaccent_wrapper(self.env.cr)
query = """SELECT id
FROM res_partner
{where} ({email} {operator} {percent}
OR {display_name} {operator} {percent}
OR {reference} {operator} {percent})
-- don't panic, trust postgres bitmap
ORDER BY {display_name} {operator} {percent} desc,
{display_name}
""".format(where=where_str,
operator=operator,
email=unaccent('email'),
display_name=unaccent('display_name'),
reference=unaccent('ref'),
percent=unaccent('%s'))
where_clause_params += [search_name]*4
if limit:
query += ' limit %s'
where_clause_params.append(limit)
self.env.cr.execute(query, where_clause_params)
partner_ids = map(lambda x: x[0], self.env.cr.fetchall())
if partner_ids:
return self.browse(partner_ids).name_get()
else:
return []
return super(Partner, self).name_search(name, args, operator=operator, limit=limit)
the error line :
if partner_ids:
return self.browse(partner_ids).name_get()
else:
return []