This question has been flagged
2 Replies
4012 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

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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.

Author Best Answer

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

Avatar
Discard