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

时间:2013-01-13 09:27:21

标签: python openerp

我想通过其他对象向对象account.bank.statement.line添加行,但我收到此错误:"dictionary update sequence element #0 has length 3; 2 is required"

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

5 个答案:

答案 0 :(得分:63)

由于您尝试使用错误的序列(dictlist)结构更新tuple对象,因此出现此错误。

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)
>>> 

答案 1 :(得分:6)

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

WITH minmax AS 
 ( -- create a list of all existing start/end values (possible to simplify using Unpivot or Cross Apply)
   SELECT Min AS val, -1 AS prio, 1 AS flag  -- main table, range start
   FROM test_table
   UNION ALL
   SELECT Max+1,   -1, -1                      -- main table, range end
   FROM test_table
   UNION ALL 
   SELECT Min, 1,  1                      -- reference table, adjusted range start
   FROM reference_table
   UNION ALL
   SELECT Max+1, 1, -1                      -- reference table, adjusted range end
   FROM reference_table
 )
, all_ranges AS 
 ( -- create all ranges from current to next row
   SELECT minmax.*,
     Lead(val) Over (ORDER BY val, prio desc, flag) AS next_val,  -- next value = end of range
     Sum(flag) Over (ORDER BY val, prio desc, flag ROWS Unbounded Preceding) AS Cnt -- how many overlapping periods exist
   FROM minmax
 )
SELECT val, next_val-1
FROM all_ranges
WHERE Cnt = 1           -- 1st level only
  AND prio + flag =  0  -- either (prio -1 and flag  1) = range start in base table
                        --     or (prio  1 and flag -1) = range end in ref table
ORDER BY 1

代替

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

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

我得到了dictionary update sequence element #0 has length 3; 2 is required
当我尝试使用 .values()

将 dict 转换为列表时

使用.items()解决了

list(dict(new_row.items()))