Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

I'm trying to create a module to group accounting entries by analytical accounts in both the list view and the pivot view. This is my code:

With new databases, it works perfectly. I can group the accounting entries by analytical account and I can see the account names. However, when they are in production databases with more than 10,000 entries, two things happen...

1: If I leave auto_init on, it installs without problems, but when grouping, it groups everything as False and in the pivot view, it shows it as None.

2: If I remove the init, using the True store, it recalculates all the records to the point that it can't recalculate them. The service gets stuck, and the module doesn't even install or update.

Any recommendations? Thanks!

# -*- coding: utf-8 -*-
from odoo import models, fields, api
import logging
_logger = logging.getLogger(__name__)
import json
from odoo.tools.sql import column_exists, create_column

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

analytic_account_names = fields.Char(
string="Cuentas Analíticas asdasd",
compute="_compute_analytic_account_names",
store=True,
index=True,
precompute=True,
)

def _auto_init(self):
"""
Create compute stored field check_number
here to avoid MemoryError on large databases.
"""
if not column_exists(self.env.cr, 'account_move_line', 'analytic_account_names'):
create_column(self.env.cr, 'account_move_line', 'analytic_account_names', 'varchar')

return super()._auto_init()

@api.depends('analytic_distribution')
def _compute_analytic_account_names(self):
AnalyticAccount = self.env['account.analytic.account'].sudo()
all_ids = set()
for line in self:
ids = self._extract_analytic_ids(line.analytic_distribution)
all_ids.update(ids)
all_accounts = AnalyticAccount.browse(all_ids).exists()
id_to_name = {acc.id: acc.name for acc in all_accounts}
for line in self:
ids = self._extract_analytic_ids(line.analytic_distribution)
names = [id_to_name.get(acc_id, 'Desconocido') for acc_id in ids]
line.analytic_account_names = ', '.join(sorted(names)) if names else ''

def _extract_analytic_ids(self, raw):
ids = set()
if isinstance(raw, dict):
keys = raw.keys()
elif isinstance(raw, str):
try:
parsed = json.loads(raw)
if isinstance(parsed, dict):
keys = parsed.keys()
else:
keys = []
except Exception:
keys = raw.split(',') if ',' in raw else []
else:
keys = []

for key in keys:
try:
ids.add(int(str(key).strip()))
except (ValueError, TypeError):
continue

return ids

​​
Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
lip 24
4654
4
sie 25
2188
2
maj 24
2129
0
gru 23
1509
1
wrz 23
2087