Odoo 8 - 如何将product.template many2one属性名称添加到product.product name_get

时间:2015-06-24 22:16:51

标签: odoo-8

您好我找到了几个答案,只更改了一个型号中的name_get,但没有找到product.product name_get def ...中的product.template many2one属性。

试过这个:

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 = context.get('display_default_code', True) and d.get('default_code',False) or False
        attribute_id = product.product_tmpl_id.attribute_id
        if code:
            name = '[%s] %s - %s' % (code,attribute,name)
        return (d['id'], name)

这很好用,但只返回属性的id,而不是它的名字......

编辑: 这是关于模块https://github.com/OCA/product-attribute/tree/8.0/product_brand 我想将品牌名称添加到产品名称功能中,以便在POS和采购订单表格中将其添加到产品名称中。 ...

“[default_code]品牌名称 - 产品名称”

2 个答案:

答案 0 :(得分:1)

这是它的工作原理

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','')
        brand = product.product_tmpl_id.product_brand_id.name
        code = context.get('display_default_code', True) and d.get('default_code',False) or False
        if brand:
            name = '%s - %s' % (brand,name)
        if code:
            name = '[%s] - %s' % (code,name)
        return (d['id'], name)

答案 1 :(得分:0)

尝试以下,

def name_get(self, cr, user, ids, context=None):
    res = []
    if context is None:
        context = {}
    if isinstance(ids, (int, long)):
        ids = [ids]
    if not len(ids):
        return []

    for r in self.browse(cr, user, ids, context=context):
        name = r.name
        code = r.default_code
        attr_name = r.product_tmpl_id.product_brand_id and r.product_tmpl_id.product_brand_id.name or ''
        value = '[%s] %s - %s' % (code,attr_name,name) 
        res.append((r.id, name))
    return res
相关问题