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

Odoo 16中ir.actions.server报NoneType无serveraction属性的解决咨询

解决Odoo服务器动作报错并实现登录后打开新记录表单

问题根源

你当前的错误是因为:用@api.model装饰的是类方法,这类方法属于模型本身而非具体记录集,但你在服务器动作里调用records.serveraction()——当没有选中任何记录(比如登录触发的场景),records就是None,自然会抛出'NoneType' object has no attribute 'serveraction'的异常。

正确实现步骤

1. 修正Python方法

保持@api.model装饰,但方法内部直接通过模型创建新记录并返回打开表单的动作,完全不依赖records参数:

from odoo import models, api

class KsKassenbuch(models.Model):
    _name = 'ks_sale.kskassenbuch'
    _description = 'KS Kassenbuch'

    # 你的字段定义...

    @api.model
    def open_new_record_form(self):
        # 创建空的新记录(可根据需求传入默认字段值)
        new_record = self.create({})
        # 返回打开新记录表单的动作
        return {
            'type': 'ir.actions.act_window',
            'name': '新Kassenbuch记录',
            'res_model': self._name,
            'res_id': new_record.id,
            'view_mode': 'form',
            'target': 'current',  # 用'new'会弹出新窗口,按需选择
        }

2. 配置XML服务器动作

服务器动作必须关联到目标模型,且执行代码要直接调用模型的类方法,而非records:

<record id="action_server_open_new_kskassenbuch" model="ir.actions.server">
    <field name="name">打开新Kassenbuch表单</field>
    <field name="model_id" ref="model_ks_sale_kskassenbuch"/>
    <field name="state">code</field>
    <field name="code">
        # 直接通过环境调用模型的类方法
        env['ks_sale.kskassenbuch'].open_new_record_form()
    </field>
</record>

3. 绑定登录触发(可选)

如果要实现登录后自动执行:

  • 可以将该服务器动作设置为用户的默认登录动作,通过修改res.users的action_id字段实现
  • 或者通过ir.rules或自动动作(ir.actions.automation)绑定登录事件触发

核心注意事项

  • 类方法必须通过env['model_name']调用,不能用records,无选中记录时records为空
  • 返回的动作字典必须包含type、res_model、res_id、view_mode这些必填项
  • 确保XML中的model_id引用正确,与你的模型完全匹配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 11:11:14