OpenERP - 哪个字段从重写的name_get()函数中获取值?

时间:2015-02-09 21:22:15

标签: openerp odoo

我希望这个问题有道理。我的目标是显示name_get()中定义的字段。我在mrp_bom类中重写了name_get()函数,附加了代码。但是,我不知道哪个字段将从函数name_get()获取返回值。非常感谢任何见解!

class mrp_bom(osv.osv):
    _inherit = 'mrp.bom'
    _name = 'mrp.bom'
    _columns = {
        'x_nk_default_code': fields.related('product_id', 'default_code', 
            type='char', relation='product.product', 
            string='Part Number', store=True, 
            readonly=True),
        'x_nk_class_desc': fields.related('product_id', 'categ_id', 'name', 
            type='char', string='Class Description', 
            store=True, readonly=True),
        'x_nk_item_desc': fields.related('product_tmpl_id', 'name', 
            type='char', relation='product.template', 
            string='Item Description', store=True, 
            readonly=True),
        'categ_id': fields.related('product_id', 'categ_id', type='integer', 
            relation='product.product', string='Categ_ID', 
            store=True, readonly=True),
        'x_category_code': fields.related('product_id', 'categ_id', 
            'x_category_code', type='char', string='Class 
            Description', store=True, readonly=True),
    }


    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('x_category_code',False) or False
            if code:
                name = '[%s] %s' % (code,name)
            return (d['id'], name)
        result = []
        for product_category in self.browse(cr, user, ids, context=context):
            mydict = {
                'id': product_category.id,
                'name': product_category.name,
                'x_category_code': 
                product_category.x_category_code,
            }
        result.append(_name_get(mydict))
    return result

1 个答案:

答案 0 :(得分:3)

name_get方法用于在many2one字段中显示记录的值。例如,在销售订单行中,如果选择产品,则字段“product_id”的销售订单行中显示的值必须是product.product对象上“name_get”的结果。

没有特殊字段显示name_get的结果。如果您需要将name_get方法的结果放在记录的字段中,则应使用属性“compute”创建:http://odoo-new-api-guide-line.readthedocs.org/en/latest/fields.html#computed-fields

您可以在此处找到更多信息:http://odoo-new-api-guide-line.readthedocs.org/en/latest/environment.html?highlight=name_get

我希望这对你有所帮助。

相关问题