Skip to Content
Menu
This question has been flagged

Currently I'm in need of adding a Float compute or related field (both will give me the same result). But I do need the field to be stored. The problem with storing this field is that the model I am trying to update, the stock.move model, currently has a lot of records. So whenever I push my code with the field the instance fails to build as it runs out of memory trying to update all the records within stock.move. I also don't need to update every single record within stock.move. This field will give me added functionality for the future. Is there anyway to limit which records Odoo is trying to update as part of the storing of the field?

Avatar
Discard
Best Answer

Hi,

You can add the field to the database table using SQL query, which will skip the computation for the existing records. 


See an optimization proposed here: https://github.com/odoo/odoo/pull/120328/files

Sample from odoo source code:


class AccountMoveLine(models.Model):
_inherit = "account.move.line"

l10n_pe_group_id = fields.Many2one("account.group", related="account_id.group_id", store=True)

def _auto_init(self):
"""
Create column to stop ORM from computing it himself (too slow)
"""
if not
column_exists(self.env.cr, self._table, 'l10n_pe_group_id'):
create_column(self.env.cr, self._table, 'l10n_pe_group_id', 'int4')
self.env.cr.execute("""
UPDATE account_move_line line
SET l10n_pe_group_id = account.group_id
FROM account_account account
WHERE account.id = line.account_id
"""
)
return super()._auto_init()


Or you can initially add the field as a non computed field in the module and install in the db and then make it as a computed field, this will also do the trick.


Thanks

Avatar
Discard
Related Posts Replies Views Activity
2
Sep 23
1849
1
Aug 23
1250
1
Jan 23
1268
1
Jul 22
1652
0
May 22
1898