Python Odoo:one2many doen';api的变化取决于
我有3个模型,下面是每个模型的有趣部分: 投票: 历史线:Python Odoo:one2many doen';api的变化取决于,python,python-2.7,openerp,odoo-8,Python,Python 2.7,Openerp,Odoo 8,我有3个模型,下面是每个模型的有趣部分: 投票: 历史线: class history_line(osv.osv) _name = 'history_line' _columns = { fieldsA = fields.integer('First field'), fieldB = fields.integer('Second field'), this_id = fields.many2one('res.partner', 'link to res'), }
class history_line(osv.osv)
_name = 'history_line'
_columns = {
fieldsA = fields.integer('First field'),
fieldB = fields.integer('Second field'),
this_id = fields.many2one('res.partner', 'link to res'),
}
合作伙伴:
class res_partner(osv.osv)
_inherit = 'res.partner'
_columns = {
vote_partner_ids = fields.one2many('vote',
'name',
'Voted peoples',
select=True),
vote_history_ids = fields.one2many('history.line',
'this_id',
compute='compute_type',
string='History of votes'),
}
@api.multi
@api.depends('vote_partner_ids.type')
def compute_type(self):
for record in self:
if self.vote_partner_ids.type:
record.vote_history_ids = [(0, 0, {'self.vote_history_ids.fieldA': self.vote_partner_ids.type + 4,
'self.vote_history_ids.fieldB': self.vote_partner_ids.type - 2})]
它们也是新历史记录行的默认值(例如,当您不做任何事情时fieldA=1,当您不做任何事情时fieldB=-1)
我不能把我的计算函数移到别处,因为这里计算了很多东西
问题是:当我修改投票类型时,将创建一个新的历史记录行,但使用默认值。我不能让这些值变成其他值。(即使我直接输入一个值,如10)
那么,为什么要选择默认值,即使我要求计算它们,但它仍然知道必须创建一个新的元组?您不必创建要更改的字段的“路径”,删除self.vote\u history\u id。在要更改的字段之前的字段中:
@api.multi
@api.depends('vote_partner_ids.type')
def compute_type(self):
for record in self:
if self.vote_partner_ids.type:
record.vote_history_ids = [(0, 0, {'fieldA': self.vote_partner_ids.type + 4,
'fieldB': self.vote_partner_ids.type - 2})]
这里不能使用内部循环self,而将使用record。
@api.multi
@api.depends('vote_partner_ids.type')
def compute_type(self):
for record in self:
if self.vote_partner_ids.type:
record.vote_history_ids = [(0, 0, {'fieldA': self.vote_partner_ids.type + 4,
'fieldB': self.vote_partner_ids.type - 2})]