101

他のオブジェクトを介してオブジェクトに行を追加したいのですが、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
4

5 に答える 5

97

dictこのエラーは、間違ったシーケンス (listまたはtuple) 構造を使用してオブジェクトを更新しようとしたために発生しました。

cash_id.create(cr, uid, lines,context=None)linesdict オブジェクトに変換しようとしています:

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

このタプルから 2 番目のゼロを削除して、正しく dict オブジェクトに変換します。

自分でテストするには、これを python シェルで試してください。

>>> 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)
于 2013-01-14T04:38:16.057 に答える
7

等しい長さのタプルから dict を作成する高速な方法の 1 つ:

>>> t1 = (a,b,c,d)
>>> t2 = (1,2,3,4)
>>> dict(zip(t1, t2))
{'a':1, 'b':2, 'c':3, 'd':4, }
于 2019-05-30T11:03:36.310 に答える
3

特定の質問に対する実際の回答ではありませんが、私のように fastAPI でこのエラーが発生し、ここにたどり着く人が他にいる場合:

ルート レスポンスに、JSON でシリアル化できない値が含まれている可能性がありますjsonable_encoder。私にとっては WKBElement でした: https://github.com/tiangolo/fastapi/issues/2366

問題のように、出力から値を削除するだけになりました。

于 2021-02-06T23:40:27.017 に答える