Odoo 8:来自计算值的Many2One

时间:2017-09-21 08:35:59

标签: python openerp odoo-8 openerp-8

我有来自外部来源的数据进入我的模型。 ' Log' model获取员工ID,日志消息和时间戳。在某些情况下,日志消息中包含位置ID(指向res.partner,用于传递地址)。

我使用python函数从字符串中获取id,从' compute'中调用它。字段中的参数,工作正常。 现在我想要做的是使用该ID链接到res.partner,这样该位置的名称将显示在表格中,而不是ID,并链接到正确的地址。

class Log(models.Model):
_name = "MyModel.log"

employee_id = fields.Many2one('hr.employee', 'Employee')
timestamp = fields.Date('Timestamp')
message = fields.Char('Log message')
location_id = fields.Char(string='Location', compute='_get_location_id')

@api.one
@api.onchange('message')
def _get_location_id(self):
    searchedFor = "id = "
    stringToBeSearched = self.message
    result = stringToBeSearched.find(searchedFor)

    if result != -1:
        numbers = sum(c.isdigit() for c in stringToBeSearched)
        loc_id = stringToBeSearched[result + 5:result + 5 + numbers -1]
        self.location_id = loc_id
        print(loc_id)
    return

问题是:当使用Char字段时,ID显示正常,但如果字段类型更改为Many2One,则不显示任何内容,尽管值正确计算(我看到打印在控制台中显示) 。 我知道' Many2One'字段不参加“计算”。参数,但那我应该怎么做?

提前致谢!

0 个答案:

没有答案