5

我有一个像这样的 many2one 字段

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

它向我显示所有产品名称(来自 product_template 类的名称属性)并在其中存储 id。我想在里面显示产品的 part_number 而不是 name 和 store id。有没有办法在 openerp7. 谢谢你的时间。

4

3 回答 3

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 或产品没有部件号,它会优雅地处理它。

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

于 2013-07-27T10:52:13.647 回答
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

但是这个特殊功能的问题是“这是一个全球性的变化”,无论你在哪里列出产品,都会显示为Product Name - Part Number。如果有人可以针对特定于上下文的操作做得更好,那就太好了。

热切欢迎进一步的回答。:)

于 2013-08-02T04:26:40.587 回答
-2

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

如果您获得属性,请使用:

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

希望你用。问候

于 2013-07-26T16:23:30.903 回答