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

如何通过Postman表单数据在Odoo中存储多条product_char数据

解决Odoo API批量插入多条product_char数据的问题

问题根源

Postman提交多个同名product_char表单字段时,直接用rec['product_char']只能拿到最后一个值——因为request.params会自动覆盖重复键的内容,没法获取全部条目。

修正后的代码

改用表单列表获取方法拿到所有product_char值,再循环创建明细记录:

@http.route(['/api/post/petty_cash/'], type='http', auth="none", methods=['POST'], csrf=False)
@check_valid_token
def create_petty_cash(self, **rec):
    user_id = request.uid

    data_header = {
        'reference' : rec['reference'],
        'partner_id' : rec['partner_id'],
        # 'state' : 'manager_app',
        'request_by' : user_id,
        'date_requested' : fields.Date.today(),
        # 'attachment' : rec['attachment'],
    }
    petty_cash = request.env['petty.cash.app'].sudo().create(data_header)

    # 获取所有提交的product_char值
    product_chars = request.httprequest.form.getlist('product_char')
    # 循环创建每条明细行
    for product_char in product_chars:
        data_line = {
            'pety_cash_id' : petty_cash.id,
            'product_char' : product_char.strip()  # 可选:清理首尾空格
        }
        request.env['petty.cash.line'].sudo().create(data_line)
    
    # 可选:返回创建结果(JSON格式)
    return request.make_response(json.dumps({
        'status': 'success',
        'petty_cash_id': petty_cash.id,
        'lines_created': len(product_chars)
    }), headers={'Content-Type': 'application/json'})

核心改动说明

  • request.httprequest.form.getlist('product_char'):专门用于提取表单中同名字段的所有值,返回一个包含所有条目的列表
  • 遍历列表逐个创建petty.cash.line记录,确保每个product_char都能存入数据库
  • 如果你的请求是JSON格式(而非表单提交),则替换为product_chars = request.jsonrequest.get('product_char', [])来获取列表值

内容的提问来源于stack exchange,提问作者Bimo Anugrah Prasetyo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 20:57:27