2013-07-26 6 views
5

의 many2one에서 다른 필드 나는 그것은 내부의 모든 제품 이름 (product_template 클래스의 이름 속성) 저장 아이디 나 표시이표시 이름 대신

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

같은 many2one 필드가 있습니다. 이름과 상점 ID 대신 제품의 part_number를 표시하고 싶습니다. 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와의 #end 사이의 코드 내 코드 조각이다)

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 

수행하지만이 특정 기능에 문제가 "그것은 세계적인 변화의 "제품을 나열하면 언제든지 제품명 - 부품 번호이 표시됩니다. 누군가가 컨텍스트 특정 작업에 대해 더 잘 수행 할 수 있으면 좋을 것입니다.

기타 답변은 열렬히 환영합니다. :)

관련 문제