简体   繁体   中英

Odoo automatic method call in model

I have this model:

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")

I have this python dict:

    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}
    ]

And this method:

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)

All works fine. But, I need automatic execution of this method on Data class call from tree view, report..., to fill out the table before anything else.

If table aibot.data is to hold report data lines of the report table, you can do the following to fill in automatically when the report object record will be created:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM