模型中的Odoo自动方法调用



我有这个型号:

class Data(models.Model):
_name = 'aibot.data'
_description = 'aibot.data'
symbol = fields.Char(string='Symbol', required=False)
ref = fields.Float(string='Amount', required=False)
amount = fields.Float(string='Amount', required=False)
user_id = fields.Many2one('res.users', string='User', required=True, ondelete='cascade',
default=lambda self: self.env.uid, help="User")

我有一条python格言:

m = [
{'symbol': '2', 'ref': 7.8,  'amount': 87},
{'symbol': '2', 'ref': 7.8,  'amount': 25},
{'symbol': '2', 'ref': 7.8,  'amount': 31},
{'symbol': '2', 'ref': 7.8,  'amount': 26},
{'symbol': '2', 'ref': 7.8,  'amount': 90},
{'symbol': '2', 'ref': 7.8,  'amount': -18}
]

这种方法:

def-rep(self(:

parse = 1
self.search([('create_uid', '=', 'user.id')]).unlink()
m = [
{'symbol': '2', 'ref': 7.8,  'amount': 87},
{'symbol': '2', 'ref': 7.8,  'amount': 25},
{'symbol': '2', 'ref': 7.8,  'amount': 31},
{'symbol': '2', 'ref': 7.8,  'amount': 26},
{'symbol': '2', 'ref': 7.8,  'amount': 90},
{'symbol': '2', 'ref': 7.8,  'amount': -18}
]
for i in m:
print('hola', '')
self.env['aibot.data'].create(i)

一切都很好。但是,我需要在树视图、报表…的Data类调用上自动执行此方法。。。,先填好表格,然后再做其他事情。

如果表aibot.data要保存报表表的报表数据行,则可以在创建报表对象记录时执行以下操作自动填写:

m = [
{'symbol': '2', 'ref': 7.8,  'amount': 87},
{'symbol': '2', 'ref': 7.8,  'amount': 25},
{'symbol': '2', 'ref': 7.8,  'amount': 31},
{'symbol': '2', 'ref': 7.8,  'amount': 26},
{'symbol': '2', 'ref': 7.8,  'amount': 90},
{'symbol': '2', 'ref': 7.8,  'amount': -18}
]
class Report(models.Model):
_name = 'aibot.report'
_description = 'this is the report object'
# data lines for the report
data_ids = fields.One2many('aibot.data','report_id')
@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
data_ids = []
for i in m:
data_ids.append((0,0,i))
res['data_ids'] = data_ids
return res
class Data(models.Model):
_name = 'aibot.data'
report_id = fields.Many2one('aibot.report')
symbol = fields.Char(string='Symbol', required=False)
ref = fields.Float(string='Amount', required=False)
amount = fields.Float(string='Amount', required=False)

最新更新