2013-07-26 85 views
5

另一场我有一个many2one场这样显示在many2one而不是名称

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

它显示了我里面的所有产品名称(从product_template类的名称属性)并存储ID。我想显示产品的part_number而不是名称和商店ID。有没有办法在openerp7中做到这一点。谢谢你的时间。

回答

-1

当您使用浏览方法时,您将获得产品的所有信息数据。

,如果你得到属性使用:

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

我希望大家使用。问候

5

有几种方法可以做到这一点。

显示关系字段的名称是name_get方法的结果。您可以继承product.product并覆盖name_get但这会影响整个系统,每次显示产品时都会改变这一点。你可以使用context对此进行编码,但它开始变得混乱。

如果你只是想要代码,我会创建一个像这样的相关领域。

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

然后在您的表单上使用它。 ORM图层非常聪明,如果您的特定记录没有产品ID或产品没有产品编号,它会优雅地处理它。

如果您需要一点技巧,您可以创建一个功能字段,例如显示名称和部件号。

+0

谢谢阿德里安,这真的很好,很有用。但是我的意图是从现有产品列表中搜索零件号(而不是名称,想要按零件号搜索)。如果我指定一个many2one字段,我可以输入名称的前几个字母,并且匹配的产品名称将被删除,在我的情况下,我需要通过零件号搜索。希望我已经清楚地解释了我的需求。请给我一些实施该建议的建议。谢谢 – Vivek

0

我的基本意图是显示所有产品的部件号,并提供按部件号搜索的功能。

我做到了这样的product.product类(#Vivek和#结束之间的代码是我的代码段)

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 

但与此特定功能的问题是,“这是一个全球性的变化“无论您在哪里列出产品,显示屏都会是产品名称 - 部件号。如果某人能够更好地进行情境特定操作,那将会很棒。

热烈欢迎更多的答案。 :)

相关问题