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

如何在Odoo15中实现数据一次性创建后仅允许更新

在Odoo15中实现单条记录仅允许更新的方案

1. 定义业务模型

创建专门的配置模型,通过重写create方法限制仅能创建一条记录,同时配合SQL约束加强唯一性限制:

from odoo import models, fields, api, exceptions

class AppConfiguration(models.Model):
    _name = 'app.configuration'
    _description = '系统全局配置'
    _rec_name = 'config_name'

    config_name = fields.Char(string='配置名称', default='系统全局配置', readonly=True)
    max_login_attempts = fields.Integer(string='最大登录尝试次数', default=5)
    auto_logout_minutes = fields.Integer(string='自动登出分钟数', default=30)
    is_default = fields.Boolean(string='默认配置', default=True, readonly=True)

    # SQL约束确保仅一条默认配置
    _sql_constraints = [
        ('unique_default_config', 'UNIQUE(is_default)', '系统仅允许存在一条默认配置记录!')
    ]

    @api.model
    def create(self, vals):
        # 检查是否已存在配置记录
        existing_config = self.search([], limit=1)
        if existing_config:
            raise exceptions.UserError("配置记录已存在,请直接编辑现有记录,无需新增!")
        return super(AppConfiguration, self).create(vals)

    @api.model
    def get_global_config(self):
        """获取唯一的全局配置记录,不存在则自动创建"""
        config = self.search([], limit=1)
        if not config:
            config = self.create({})
        return config

2. 配置视图与菜单

设置表单视图为默认视图,让用户直接进入编辑模式,避免误操作尝试新增:

<odoo>
    <data>
        <!-- 表单视图 -->
        <record id="view_app_config_form" model="ir.ui.view">
            <field name="name">app.configuration.form</field>
            <field name="model">app.configuration</field>
            <field name="arch" type="xml">
                <form string="系统全局配置">
                    <sheet>
                        <group>
                            <field name="config_name"/>
                            <field name="max_login_attempts"/>
                            <field name="auto_logout_minutes"/>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- 菜单动作 -->
        <record id="action_app_config" model="ir.actions.act_window">
            <field name="name">全局配置</field>
            <field name="res_model">app.configuration</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_app_config_form"/>
            <field name="target">current</field>
            <field name="context">{'form_view_initial_mode': 'edit'}</field>
        </record>

        <!-- 菜单 -->
        <menuitem id="menu_app_config" name="系统配置" parent="base.menu_config" action="action_app_config"/>
    </data>
</odoo>

3. 业务逻辑中调用配置

在其他业务代码中,可通过get_global_config方法直接获取唯一配置记录,无需担心多记录问题:

# 在其他模型中调用配置
config = self.env['app.configuration'].get_global_config()
max_attempts = config.max_login_attempts

这套方案下,用户无法手动新增多条配置记录,只能对已存在的记录进行编辑;通过API或导入操作时,也会被create方法的校验阻止,配合SQL约束进一步保障数据唯一性。

内容的提问来源于stack exchange,提问作者Abdus Sattar Bhuiyan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 22:15:39