【问题标题】:dictionary update sequence element #0 has length 3; 2 is required字典更新序列元素#0的长度为3; 2 是必需的
【发布时间】:2023-04-06 00:00:01
【问题描述】:

我想通过其他对象将行添加到对象account.bank.statement.line 但我收到以下错误:

"字典更新序列元素#0的长度为3;需要2"

这是我的代码:

def action_account_line_create(self, cr, uid, ids):
    res = False
    cash_id = self.pool.get('account.bank.statement.line')
    for exp in self.browse(cr, uid, ids):
        company_id = exp.company_id.id
        #statement_id = exp.statement_id.id
        lines = []
        for l in exp.line_ids:
            lines.append((0, 0, {
                'name': l.name,
                'date': l.date,
                'amount': l.amount,
                'type': l.type,
                'statement_id': exp.statement_id.id,
                'account_id': l.account_id.id,
                'account_analytic_id': l.analytic_account_id.id,
                'ref': l.ref,
                'note': l.note,
                'company_id': l.company_id.id
            }))

        inv_id = cash_id.create(cr, uid, lines,context=None)
        res = inv_id
    return res 

我改变了它,但后来我遇到了这个错误:

  File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\workflow\wkf_expr.py", line 68, in execute
  File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\workflow\wkf_expr.py", line 58, in _eval_expr
  File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\tools\safe_eval.py", line 241, in safe_eval
  File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\tools\safe_eval.py", line 108, in test_expr
  File "<string>", line 0   
   ^
SyntaxError: unexpected EOF while parsing

代码:

def action_account_line_create(self, cr, uid, ids, context=None):
    res = False
    cash_id = self.pool.get('account.bank.statement.line')
    for exp in self.browse(cr, uid, ids):
        company_id = exp.company_id.id
        lines = []
        for l in exp.line_ids:
            res = cash_id.create ( cr, uid, {
                'name': l.name,
                'date': l.date,
                'amount': l.amount,
                'type': l.type,
                'statement_id': exp.statement_id.id,
                'account_id': l.account_id.id,
                'account_analytic_id': l.analytic_account_id.id,
                'ref': l.ref,
                'note': l.note,
                'company_id': l.company_id.id
            }, context=None)
    return res

【问题讨论】:

  • 您当前的对象/类是什么?您想直接创建线还是想在当前对象中将线添加为 one2many?这里的问题是您不能在 create() 中传递列表。你必须通过字典。
  • 感谢您的回复,我将 hr_expense_expense 更改为在 state:'confirm' 后直接在 table account_bank_statement_line 上添加行

标签: python odoo


【解决方案1】:

出现此错误是因为您尝试使用错误的序列(listtuple)结构更新 dict 对象。

cash_id.create(cr, uid, lines,context=None) 试图将 lines 转换为 dict 对象:

(0, 0, {
    'name': l.name,
    'date': l.date,
    'amount': l.amount,
    'type': l.type,
    'statement_id': exp.statement_id.id,
    'account_id': l.account_id.id,
    'account_analytic_id': l.analytic_account_id.id,
    'ref': l.ref,
    'note': l.note,
    'company_id': l.company_id.id
})

从这个元组中删除第二个零以正确地将其转换为 dict 对象。

要自己测试,请在 python shell 中尝试:

>>> l=[(0,0,{'h':88})]
>>> a={}
>>> a.update(l)

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    a.update(l)
ValueError: dictionary update sequence element #0 has length 3; 2 is required

>>> l=[(0,{'h':88})]
>>> a.update(l)

【讨论】:

    【解决方案2】:

    当我使用错误的语法更新字典时出现此错误:

    试试这些:

    lineItem.values.update({attribute,value})
    

    而不是

    lineItem.values.update({attribute:value})
    

    【讨论】:

    • 这应该是公认的答案,为了大家从谷歌来到这里
    • @YoshiAskharoun 接受的答案是帮助 OP,而不是帮助其他人
    【解决方案3】:

    从等长元组创建字典的快速方法之一:

    >>> t1 = (a,b,c,d)
    >>> t2 = (1,2,3,4)
    >>> dict(zip(t1, t2))
    {'a':1, 'b':2, 'c':3, 'd':4, }
    

    【讨论】:

      【解决方案4】:

      不是对具体问题的真正答案,但如果有其他人,比如我,在 fastAPI 中遇到此错误并最终出现在此处:

      这可能是因为您的路由响应中的值无法通过jsonable_encoder 进行 JSON 序列化。对我来说是 WKBElement:https://github.com/tiangolo/fastapi/issues/2366

      就像在问题中一样,我最终只是从输出中删除了值。

      【讨论】:

        【解决方案5】:

        我收到了dictionary update sequence element #0 has length 3; 2 is required
        当我尝试使用.values()将字典转换为列表时@

        使用.items()解决了这个问题

        list(dict(new_row.items()))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-17
          • 1970-01-01
          • 1970-01-01
          • 2016-06-30
          • 1970-01-01
          • 1970-01-01
          • 2015-02-22
          • 2019-08-10
          相关资源
          最近更新 更多