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

向Odoo更新数据时出现record不存在或已删除错误,求协助解决

问题排查与修复方案

核心错误原因

你遇到的 record does not exist or has been deleted (record: account.move.line(5398,), user:7) 错误,根源在于错误地将发票ID(account.move的ID)传入了one2many字段的更新命令:
你的代码中使用了 [(1, invoice_id, {...})],但Odoo的one2many字段更新语法里,(1, id, vals) 中的 id 必须是已存在的account.move.line记录ID,而非父级发票(account.move)的ID。Odoo因此尝试查找ID为5398的发票行记录,但该记录不存在,导致报错。

代码中的其他问题

  1. one2many命令格式错误:若你想新增发票行,应使用 (0, 0, vals) 而非 (1, ...);若需更新已有行,必须先获取对应发票行的ID。
  2. JSON序列化错误:将 api_invoice_line_id 转为字符串 str(api_invoice_line_id) 会破坏JSON结构,导致Odoo无法解析命令。需直接传递列表格式的数据。
  3. 请求头Content-Type错误:使用 application/jsonp 但发送的是JSON数据,应改为 application/json。
  4. 未处理空搜索结果:若 search_read 未找到匹配的发票,ids[0] 会触发索引越界错误,需先判断结果是否为空。
  5. 拼写错误:varient_name 应为 variant_name。

修复后的代码示例

import xmlrpc.client
import requests
import json
from django.shortcuts import get_object_or_404  # 假设使用Django框架

endpoint_url = "/api/account.move/"
obj = get_object_or_404(OrderItem, order__id=order_id)
invoice_date = obj.order.created_on
name = obj.product.variant_name  # 修正拼写错误
price = obj.total
quantity = obj.quantity
payment_source = obj.order.payment_method
payment_reference = obj.order.order_number

# XML-RPC认证与发票查询
common = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/common')
uid = common.authenticate(db, username, password, {})
models = xmlrpc.client.ServerProxy(f'{url}/xmlrpc/2/object')
ids = models.execute_kw(db, uid, password, 'account.move', 'search_read', 
                        [[['source_document', '=', payment_reference]]], 
                        {'fields': ['partner_id', 'id']})

# 处理空搜索结果,避免索引越界
if not ids:
    return "Invoice not found"
invoice_id = ids[0]['id']
partner_id_ = ids[0]['partner_id'][0]

headers = {
    "access-token": tokens,
    "Content-type": "application/json",  # 修正Content-Type
    "Cookie": session_id
}

# 若新增发票行,使用(0,0,vals)格式;若需更新已有行,需先获取对应line_id
api_invoice_line_ids = [(0, 0, {'name': name, 'price_unit': price, 'quantity': quantity})]

data = {
    "partner_id": partner_id_,
    "invoice_date": str(invoice_date),
    "move_type": "out_invoice",
    "__api__invoice_line_ids": api_invoice_line_ids,  # 直接传递列表格式
    "payment_source": payment_source,
    "source_document": payment_reference,
    "rider": rider_name,
    "ref": ""
}

datas_ = json.dumps(data, indent=4)
req = requests.put(f"{url}{endpoint_url}{invoice_id}", headers=headers, data=datas_)

if req.status_code == 200:
    status = "Update Successful"
else:
    status = str(req.text)
return status

额外说明

若你的需求是更新已有发票行,需先通过XML-RPC查询该发票对应的行记录ID:

line_ids = models.execute_kw(db, uid, password, 'account.move.line', 'search_read',
                             [[['move_id', '=', invoice_id]]],
                             {'fields': ['id']})
# 假设取第一行进行更新
if line_ids:
    line_id = line_ids[0]['id']
    api_invoice_line_ids = [(1, line_id, {'name': name, 'price_unit': price, 'quantity': quantity})]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 13:50:20