2013-07-26 10 views
5

Mam pole many2one jak tenWyświetlacz kolejne pole w many2one zamiast nazwy

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

To pokazuje mi wszystkie nazwy produktów (atrybut name z product_template klasy) i przechowuje id wewnątrz. Chciałbym wyświetlić numer_part_ produktu zamiast nazwy i identyfikatora sklepu wewnątrz. Czy jest sposób na zrobienie tego w openerp7. Dziękuję za Twój czas.

Odpowiedz

-1

Podczas korzystania z metody przeglądania uzyskujesz wszystkie informacje o produkcie.

jeśli uzyska właściwości użyć:

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

Mam nadzieję użyć. dotyczy

5

Istnieje kilka sposobów, aby to zrobić.

Nazwa wyświetlana dla pól relacyjnych jest wynikiem metody name_get. Możesz odziedziczyć product.product i zastąpić name_get, ale to wpłynie na cały system, za każdym razem, gdy produkt jest wyświetlany, zmieni to. Można kodować wokół tego przy użyciu context, ale zaczyna się bałagan.

Jeśli potrzebujesz tylko kodu, utworzę podobne pole.

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

, a następnie użyj tego w formularzu. Warstwa ORM jest na tyle inteligentna, że ​​jeśli dany rekord nie ma identyfikatora produktu lub produkt nie ma numeru części, poradzi sobie z nim z wdziękiem.

Jeśli chcesz być trochę trudniejszym, możesz utworzyć pole funkcjonalne, które pokazuje np. Nazwę i numer części.

+0

Dzięki Adrian, to było naprawdę dobre i użyteczne. Jednak moim zamiarem jest przeszukanie części nr z istniejącej listy produktów (zamiast nazwy, chcę wyszukać według numeru części). Jeśli podam pole many2, mogę wpisać kilka pierwszych liter nazwy, a nazwy produktów, które pasują, zostaną usunięte, w moim przypadku będę musiał wyszukiwać według numeru części. Mam nadzieję, że jasno wytłumaczyłem swoją potrzebę. Uprzejmie proszę o sugestie, aby to wdrożyć. Dzięki – Vivek

0

Moją podstawową intencją było wyświetlenie części numeru produktu dla wszystkich produktów i umożliwienie wyszukiwania według numeru części.

osiągnąłem to w ten sposób w product.product klasie (Code między #Vivek i #end są moje fragmenty kodu)

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 

Ale problem z tym konkretnym funkcjonalność jest „To jest globalna zmiana "wszędzie tam, gdzie można wyświetlić listę produktów, wyświetlana będzie nazwa produktu: Nazwa produktu - numer części:. Jeśli ktoś może to zrobić lepiej w kontekście konkretnych działań, będzie świetnie.

Dalsze odpowiedzi są mile widziane. :)

Powiązane problemy