Skip to Content
Menu
This question has been flagged

How Can I show parent child together in many2one field lookup. For example product by category tree

Avatar
Discard

Override "name_get" method and many2one field shows product- Category. similar name_get example in the core module product/product.py available.

Author

Thanks for the response. Can you please elaborate more how to override many2one field. For example i want to show something like below. parent -->child <newline> ->child2

Best Answer

I tried by applying a domain with 'not ilike' to display only the parent from partners and it worked.

I guess that you cand do this at python level too.


Avatar
Discard
Best Answer

I will explain with default openerp core module addons\product\product.py

1) In Product screen category many2one fields display with Category Name/ Parent Category Name

Based on this output Category name_get overridden

class product_category(osv.osv):

def name_get(self, cr, uid, ids, context=None):
    if isinstance(ids, (list, tuple)) and not len(ids):
        return []
    if isinstance(ids, (long, int)):
        ids = [ids]
    reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
    res = []
    for record in reads:
        name = record['name']
        if record['parent_id']:
            name = record['parent_id'][1]+' / '+name
        res.append((record['id'], name))
    return res

2) In Sale/Purchase order Product many2one fields display with [Product code] Product Name Based on this output Product name_get overridden

 _name = "product.product"

  def name_get(self, cr, user, ids, context=None):
        if context is None:
            context = {}
        if isinstance(ids, (int, long)):
            ids = [ids]
        if not len(ids):
            return []
        def _name_get(d):
            name = d.get('name','')
            code = d.get('default_code',False)
            if code:
                name = '[%s] %s' % (code,name)
            if d.get('variants'):
                name = name + ' - %s' % (d['variants'],)
            return (d['id'], name)

Hope this will help based on your requirement override name_get method.

Avatar
Discard
Related Posts Replies Views Activity
3
Jul 24
22507
3
May 23
1157
1
Apr 23
2327
3
May 24
29271
1
Sep 18
9529