Vivek
Vivek

Reputation: 3613

Display another field in many2one instead of name

I have a many2one field like this

'product_id': fields.many2one('product.product', 'Product', required=True)

It displays me all the product names (name attribute from product_template class) and stores id inside. I would like to display the part_number of the product instead of name and store id inside. Is there a way to do this in openerp7. Thanks for your time.

Upvotes: 5

Views: 7913

Answers (3)

Adrian Merrall
Adrian Merrall

Reputation: 2499

There are a couple of ways to do this.

The name that is displayed for relational fields is the result of the name_get method. You can inherit product.product and override name_get but that will affect the entire system, every time a product is displayed it will change this. You could code around this using the context but it is starting to get messy.

If you just want the code I would create a related field like this.

'product_ref': fields.related(
        'product_id',
        'part_number',
        string='Product',
        readonly=True,
        type='char',
        help = 'The product part number',
        )

and then use this on your form. The ORM layer is smart enough that if your particular record doesn't have a product ID or a product doesn't have a part number it will handle it gracefully.

If you needed to be a little trickier you could create a functional field that showed say the name and the part number for example.

Upvotes: 5

Vivek
Vivek

Reputation: 3613

My basic intention was to display Part-Number of the product for all products and give ability to search by Part-Number.

I accomplished it this way in product.product class,(Code between #Vivek and #End are my code snippets)

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)
        # Vivek
        part_number = d.get('part_number',False)
        if part_number:
            name = '%s-%s' % (name,part_number)
        #End
        elif code:
            name = '[%s] %s' % (code,name)
        elif d.get('variants'):
            name = name + ' - %s' % (d['variants'],)
        return (d['id'], name)

    partner_id = context.get('partner_id', False)

    result = []
    for product in self.browse(cr, user, ids, context=context):
        sellers = filter(lambda x: x.name.id == partner_id, product.seller_ids)
        # Vivek
        prd_temp = self.pool.get('product.template').browse(cr, user, product.id, context=context)
        # End
        if sellers:
            for s in sellers:
                mydict = {
                          'id': product.id,
                          'name': s.product_name or product.name,
                          #vivek
                          'part_number': prd_temp.part_number,
                          #End
                          'default_code': s.product_code or product.default_code,
                          'variants': product.variants
                          }
                result.append(_name_get(mydict))
        else:
            mydict = {
                      'id': product.id,
                      'name': product.name,
                      #vivek
                      'part_number': prd_temp.part_number,
                      #End
                      'default_code': product.default_code,
                      'variants': product.variants
                      }
            result.append(_name_get(mydict))
    return result

def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
    if not args:
        args = []
    if name:
        ids = self.search(cr, user, [('default_code','=',name)]+ args, limit=limit, context=context)
        if not ids:
            ids = self.search(cr, user, [('ean13','=',name)]+ args, limit=limit, context=context)
        if not ids:
            # Do not merge the 2 next lines into one single search, SQL search performance would be abysmal
            # on a database with thousands of matching products, due to the huge merge+unique needed for the
            # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
            # Performing a quick memory merge of ids in Python will give much better performance
            ids = set()
            ids.update(self.search(cr, user, args + [('default_code',operator,name)], limit=limit, context=context))
            if not limit or len(ids) < limit:
                # we may underrun the limit because of dupes in the results, that's fine
                ids.update(self.search(cr, user, args + [('name',operator,name)], limit=(limit and (limit-len(ids)) or False) , context=context))
                # vivek
                # Purpose  : To filter the product by using part_number
                ids.update(self.search(cr, user, args + [('part_number',operator,name)], limit=(limit and (limit-len(ids)) or False) , context=context))
                #End
            ids = list(ids)
        if not ids:
            ptrn = re.compile('(\[(.*?)\])')
            res = ptrn.search(name)
            if res:
                ids = self.search(cr, user, [('default_code','=', res.group(2))] + args, limit=limit, context=context)
    else:
        ids = self.search(cr, user, args, limit=limit, context=context)
    result = self.name_get(cr, user, ids, context=context)
    return result

But the problem with this particular functionality is "It's a global change" wherever you get to list the products the display will be Product Name - Part Number. If someone can do that better for context specific actions it will be great.

Further answers are eagerly welcome. :)

Upvotes: 0

mamfredym
mamfredym

Reputation: 28

when you use the browse method, you get all information data of product.

if you get properties use:

<yourobject>.product_id.name
<yourobject>.product_id.default_code
<yourobject>.product_id.type
<yourobject>.product_id.<your_property>

I hope you use. regards

Upvotes: -2

Related Questions