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

rin*_*dra 58 python odoo

我想account.bank.statement.line通过其他对象向对象添加行但是我收到此错误:account.bank.statement.line

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 
Run Code Online (Sandbox Code Playgroud)

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

  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
Run Code Online (Sandbox Code Playgroud)

码:

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
Run Code Online (Sandbox Code Playgroud)

MBa*_*rsi 72

由于您尝试dict使用错误的序列(listtuple)结构更新对象,因此引发此错误.

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
})
Run Code Online (Sandbox Code Playgroud)

从该元组中删除第二个零以将其正确转换为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)
Run Code Online (Sandbox Code Playgroud)


Ian*_*hel 11

我用错误的语法更新字典时遇到此错误:

试试这些:

lineItem.values.update({attribute,value})
Run Code Online (Sandbox Code Playgroud)

代替

lineItem.values.update({attribute:value})
Run Code Online (Sandbox Code Playgroud)

  • @YoshiAskharoun 接受的答案是帮助OP,而不是帮助其他人 (2认同)

Jor*_*ren 11

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

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

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


AB *_*bhi 5

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

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