如何使用继承在已定义的 odoo 模型的选择字段中添加一个条目
How to add one more entry in selection field of an already defined odoo model using inheritance
环境:- Odoo 9,Python 2.7
模块 A
from openerp import models, fields, api, _, exceptions
class Games(models.Model):
_name = 'moduleA.games'
game = fields.Selection([
('cricket', 'Cricket'),
('hockey', 'Hockey'),
('golf', 'Golf')],
string='Game', default='cricket', required=True
)
模块 B
from openerp import models, fields, api, _, exceptions
class ExtraGames(models.Model):
_inherit = 'moduleA.games'
def __init__(self, pool, cr):
res = super(ExtraGames,self)._columns # res = {}
super(ExtraGames,self)._columns['game'].selection.append(
('chess', 'Chess')
)
现在使用该代码,我想在已经定义的游戏列表中再添加一个游戏 Chess,但它不起作用。实际上,由于 super(ExtraGames,self)._columns,我得到了空字典 ( {} ),因此它给出了 KeyError 'game'。
我们该怎么做?
您可以使用 selection_add
:
from openerp import fields, models
class ExtraGames(models.Model):
_inherit = 'moduleA.games'
game = fields.Selection(
selection_add=[('chess', 'Chess')],
)
环境:- Odoo 9,Python 2.7
模块 A
from openerp import models, fields, api, _, exceptions
class Games(models.Model):
_name = 'moduleA.games'
game = fields.Selection([
('cricket', 'Cricket'),
('hockey', 'Hockey'),
('golf', 'Golf')],
string='Game', default='cricket', required=True
)
模块 B
from openerp import models, fields, api, _, exceptions
class ExtraGames(models.Model):
_inherit = 'moduleA.games'
def __init__(self, pool, cr):
res = super(ExtraGames,self)._columns # res = {}
super(ExtraGames,self)._columns['game'].selection.append(
('chess', 'Chess')
)
现在使用该代码,我想在已经定义的游戏列表中再添加一个游戏 Chess,但它不起作用。实际上,由于 super(ExtraGames,self)._columns,我得到了空字典 ( {} ),因此它给出了 KeyError 'game'。
我们该怎么做?
您可以使用 selection_add
:
from openerp import fields, models
class ExtraGames(models.Model):
_inherit = 'moduleA.games'
game = fields.Selection(
selection_add=[('chess', 'Chess')],
)