You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在安装Odoo模块时跳过字段计算以缩短安装耗时?

解决方案:跳过Odoo模块安装时的现有记录字段计算

步骤1:直接在数据库中创建目标字段

通过SQL手动在对应表中添加字段,绕开Odoo安装时自动批量计算填充的逻辑。

  • 针对account.move表(假设新增字段为浮点型的x_custom_field,请根据模块定义的字段类型调整):
ALTER TABLE account.move ADD COLUMN x_custom_field DOUBLE PRECISION;
  • 针对account.move.line表:
ALTER TABLE account.move.line ADD COLUMN x_custom_field_line DOUBLE PRECISION;

注意:字段类型必须和模块代码中定义的完全匹配,比如模块用Char就对应VARCHAR(255),Boolean对应BOOLEAN,避免安装时字段类型冲突。

步骤2:修改模块代码,禁用安装时的自动计算

在字段定义中添加参数,告知Odoo初始化阶段不执行计算逻辑:

from odoo import models, fields, api

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

    x_custom_field = fields.Float(
        string='自定义字段',
        compute='_compute_x_custom_field',
        store=True,
        compute_sudo=False,
        init=False  # 核心参数:安装时不填充现有记录
    )

    @api.depends('some_related_field')
    def _compute_x_custom_field(self):
        for record in self:
            record.x_custom_field = record.some_related_field * 0.1  # 替换为实际计算逻辑

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

    x_custom_field_line = fields.Float(
        string='行自定义字段',
        compute='_compute_x_custom_field_line',
        store=True,
        compute_sudo=False,
        init=False
    )

    @api.depends('quantity', 'price_unit')
    def _compute_x_custom_field_line(self):
        for record in self:
            record.x_custom_field_line = record.quantity * record.price_unit * 0.05  # 替换为实际计算逻辑

解释:init=False会阻止Odoo在模块安装/更新时遍历所有旧记录执行计算;compute_sudo=False确保计算逻辑仅在用户操作或手动触发时运行。

步骤3:安装模块并手动触发现有记录计算

模块安装完成后,通过以下两种方式批量计算旧记录的字段值:

方法1:界面批量操作

先在模块中添加批量计算动作:

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

    def action_compute_custom_field(self):
        self._compute_x_custom_field()

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

    def action_compute_custom_field_line(self):
        self._compute_x_custom_field_line()

再在视图文件中注册动作(以account_move_views.xml为例):

<record id="action_compute_move_field" model="ir.actions.server">
    <field name="name">计算凭证自定义字段</field>
    <field name="model_id" ref="account.model_account_move"/>
    <field name="state">code</field>
    <field name="code">records.action_compute_custom_field()</field>
</record>

<record id="action_compute_line_field" model="ir.actions.server">
    <field name="name">计算凭证行自定义字段</field>
    <field name="model_id" ref="account.model_account_move_line"/>
    <field name="state">code</field>
    <field name="code">records.action_compute_custom_field_line()</field>
</record>

之后在Odoo界面打开对应列表视图,全选记录后通过「动作」菜单执行计算。

方法2:后台脚本批量计算

通过Odoo Shell分批次处理,避免一次性处理50万条记录导致数据库超时:

# 进入Odoo Shell
odoo-shell -d 你的数据库名

执行计算代码:

# 分批次处理account.move
batch_size = 10000
total_moves = env['account.move'].search_count([])
for i in range(0, total_moves, batch_size):
    move_batch = env['account.move'].search([], offset=i, limit=batch_size)
    move_batch._compute_x_custom_field()

# 分批次处理account.move.line
total_lines = env['account.move.line'].search_count([])
for i in range(0, total_lines, batch_size):
    line_batch = env['account.move.line'].search([], offset=i, limit=batch_size)
    line_batch._compute_x_custom_field_line()

关键注意事项

  • 务必保证SQL创建的字段名、类型和模块代码定义完全一致,否则会触发模块安装错误。
  • 生产环境执行批量计算前,建议先备份数据库,且选择低峰时段操作。
  • 分批次处理时可根据服务器性能调整batch_size参数,避免占用过多数据库资源。

内容的提问来源于stack exchange,提问作者Alesis Joan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 09:35:17