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

Where can I add the "city" field in search results view ?

By
Anne-Sylvie Burri
on 11/14/13, 4:33 AM 1,000 views

Hello,

I try to add the field "city" in the temporary results list for the partners. For example "Company Z (Mr Jean Dupont) Paris".

I just have modified the file "partner.py" and added "partner.city".

def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
    if not args:
        args = []
    if name and operator in ('=', 'ilike', '=ilike', 'like', '=like'):
        # 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:]
        query_args = {'name': search_name}
        limit_str = ''
        if limit:
            limit_str = ' limit %(limit)s'
            query_args['limit'] = limit
        cr.execute('''SELECT partner.id, partner.city FROM res_partner partner
                      LEFT JOIN res_partner company ON partner.parent_id = company.id
                      WHERE partner.email ''' + operator +''' %(name)s
                         OR partner.name || ' (' || COALESCE(company.name,'') || ')'
                      ''' + operator + ' %(name)s ' + limit_str, query_args)
        ids = map(lambda x: x[0], cr.fetchall())
        ids = self.search(cr, uid, [('id', 'in', ids)] + args, limit=limit, context=context)
        if ids:
            return self.name_get(cr, uid, ids, context)
    return super(res_partner,self).name_search(cr, uid, name, args, operator=operator, context=context, limit=limit)

But now I'm looking for the correct place to add "city" for the results list. In my mind it's in "search.js" but I can't find exactly where.

Please I need your help !

Thanks in advance.

JMB

0
Timo Talvitie
On 11/14/13, 8:11 AM

I'm not completely sure what you mean by temporary results, do you mean you'd like to show the city of the partner in the autocomplete dropdowns that appear when e.g. selecting a customer for a sale order? If that is the case you have to override the model's name_get function to show also the city.

Here's an example from the crm module (crm.py line 151)

def name_get(self, cr, uid, ids, context=None):
    """Overrides orm name_get method"""
    if not isinstance(ids, list) :
        ids = [ids]
    res = []
    if not ids:
        return res
    reads = self.read(cr, uid, ids, ['name', 'parent_id'], context)

    for record in reads:
        name = record['name']
        if record['parent_id']:
            name = record['parent_id'][1] + ' / ' + name
        res.append((record['id'], name))
    return res

Normally only the name would show up in the dropdown, but with this code it is customized to be preceded with the name of the parent and the character "/" (also, first a check is done to see the parent exists). You can add also other fields in a similar fashion, just pull the fields you need with self.read() first.

A BIG thank you for your answer. It's exactly what I'm looking for. I'll try to custom the code and give you a feedback later.

J.-M. Burri - Bureau ing.-conseils informatique, Anne-Sylvie Burri
on 11/15/13, 4:49 AM
0
Anne-Sylvie Burri
On 11/15/13, 4:57 AM

The code inside partner.py is not exactly the same as crm.py. I found name_get but can you help me for the syntax ?

def name_get(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = []
        for record in self.browse(cr, uid, ids, context=context):
            name = record.name
            if record.parent_id:
                name =  "%s (%s)" % (name, record.parent_id.name)
            if context.get('show_address'):
                name = name + "\n" + self._display_address(cr, uid, record, without_company=True, context=context)
                name = name.replace('\n\n','\n')
                name = name.replace('\n\n','\n')
            if context.get('show_email') and record.email:
                name = "%s <%s>" % (name, record.email)
            res.append((record.id, name))
        return res

Indeed I don't see selt.read so I don't know how to do.

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

0 follower(s)

Stats

Asked: 11/14/13, 4:33 AM
Seen: 1000 times
Last updated: 3/16/15, 8:10 AM