2015-02-06 73 views
2

我试图覆盖旧的API函数字段没有成功。 有此功能领域(display_name)与辅助方法:Odoo新的API - 覆盖旧的API函数字段

def _display_name_compute(self, cr, uid, ids, name, args, context=None): 
    context = dict(context or {}) 
    context.pop('show_address', None) 
    context.pop('show_address_only', None) 
    context.pop('show_email', None) 
    return dict(self.name_get(cr, uid, ids, context=context)) 

_display_name = lambda self, *args, **kwargs: self._display_name_compute(*args, **kwargs) 

_display_name_store_triggers = { 
    'res.partner': (lambda self,cr,uid,ids,context=None: self.search(cr, uid, [('id','child_of',ids)], context=dict(active_test=False)), 
        ['parent_id', 'is_company', 'name'], 10) 
} 

    'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers, select=True) 

我需要在这里改变,是在display_name_store_triggers,新的领域,如果使用,将触发该功能字段来计算更新。

如果我这样做的来源:

_display_name_store_triggers = { 
    'res.partner': (lambda self,cr,uid,ids,context=None: self.search(cr, uid, [('id','child_of',ids)], context=dict(active_test=False)), 
        ['parent_id', 'is_company', 'name', 'parent_root_id', 'is_branch'], 10) 
} 

然后它的作品我是如何。但我似乎无法继承该触发器并在我的模块上覆盖。

如果我这样做,我的模块:

from openerp.addons.base.res.res_partner import res_partner as res_partner_orig 

    _display_name_store_triggers = res_partner_orig._display_name_store_triggers 
    _display_name_store_triggers = { 
     'res.partner': (lambda self,cr,uid,ids,context=None: self.search(cr, uid, [('id','child_of',ids)], context=dict(active_test=False)), 
         ['parent_id', 'is_company', 'name', 'parent_root_id', 'is_branch'], 10) 
    } 

什么也没有发生。当字段parent_root_idis_branch被修改时,display_name字段不计算。

我在文档中找不到如何使用新API覆盖旧功能字段。什么是方法?

回答

3

尝试以下操作:

@api.one 
@api.depends('parent_id','is_company','name') 
def _display_name(self): 
    for partner in self: 
     <<operation on context... you need to check yourself>> 
     <<you will have to check how name_get is called and based on 
      the return value set the value as shown below>> 
     partner.display_name = <<returned value from name_get>> 

display_name = fields.Char(compute="_display_name",string="Display Name") 
+0

我已经尝试过类似的方法。它的问题在于,它不会在数据库中保存值,即使您在函数字段中添加了“store = True”。它看起来仍然使用旧功能字段并忽略此实现。同样在你的例子中你使用'api.one',但迭代就像它是多个。不应该直接从'self'访问吗?因为它的一个记录。 – Andrius 2015-02-06 10:02:03

+0

对自我的迭代并不重要,因为它是一个记录集。是的,我忘了添加'store = True'。如果我有机会,我会检查出来。但是,你能告诉我你为什么要在新的API中实现吗? – 2015-02-06 10:06:56

+0

如果新字段(我实现)被设置('is_branch','parent_root_id'),我重新实现了新的'name_get'方法来改变'display_name'。因此,如果合作伙伴是分支机构并具有'parent_root_id'(又名主合作伙伴),则它将显示像“主公司/分公司”这样的名称。问题是,只有在名称字段在写入时更改时才会更新,因为如果更改新字段,它不会触发更新。所以'display_name'没有被更新,当它应该。如果我添加需要在'_display_name_store_triggers'中触发的字段,那么它可以工作,但只有当我在源代码中执行时 – Andrius 2015-02-06 10:39:09