Search code examples
pythonpython-3.xodooodoo-13

Odoo 13 - Computed Fields - not working as expected


I have a Model ks.register has a field meter_ids which is a computed field added in XML notebook tag as a Page.

Computed field be like:

meter_ids = fields.One2many('ks.meter', string='Meters', compute="_get_register")

Below is the computed method for that field

def _get_register(self):
    ids = {}
    if not self.ids:
        return meter_ids
 query = 
    cr.execute(query)
    meter_records = cr.fetchall()
    for i in meter_records:
        ids[i[0]] = i[1] if i[1] != [None] else []
    return ids

when I go to the ks.register XML page and hit the tree view. I see

Something Went Wrong ks.register(569909,).ids Pop up error.

Can someone tell me what went wrong ?


Solution

  • Let me try to help.

    You're first searching records in ks.meter model which are link with ks.register model? For this you've created One2many field.

    So instead of writing query you can also do this. You can use Many2one field for searching in ks.meter

    for i in self:
        records = self.env['ks.meter'].search([('Many2one_field', '=', i.id)])
        getting_ids = [j.id for j in records]
        i.meter_ids = [6, 0, getting_ids]