114

I want to add lines to the object account.bank.statement.line through other object But I get following error:

"dictionary update sequence element #0 has length 3; 2 is required"

Here is my code:

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 

I changed it on that but then I ran into this error:

  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

Code:

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
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
rindra
  • 1,185
  • 2
  • 7
  • 6
  • What is your current object/class? Do you want to create line directly or you want to add line as one2many in your current object? Here problem is that you cannot pass list in create(). You must pass dictionary. – Sudhir Arya Jan 13 '13 at 09:39
  • thanks for your reply, i changed the hr_expense_expense to add line directly on the table account_bank_statement_line after state:'confirm' – rindra Jan 13 '13 at 11:22

6 Answers6

105

This error raised up because you trying to update dict object by using a wrong sequence (list or tuple) structure.

cash_id.create(cr, uid, lines,context=None) trying to convert lines into dict object:

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

Remove the second zero from this tuple to properly convert it into a dict object.

To test it your self, try this into 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)
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
MBarsi
  • 2,417
  • 1
  • 18
  • 18
30

I was getting this error when I was updating the dictionary with the wrong syntax:

Try with these:

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

instead of

lineItem.values.update({attribute:value})
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Ian Michel
  • 551
  • 6
  • 9
10

Not really an answer to the specific question, but if there are others, like me, who are getting this error in fastAPI and end up here:

It is probably because your route response has a value that can't be JSON serialised by jsonable_encoder. For me it was WKBElement: https://github.com/tiangolo/fastapi/issues/2366

Like in the issue, I ended up just removing the value from the output.

Joren Van Severen
  • 2,269
  • 2
  • 24
  • 30
  • In my case, again in fastAPI, the reason was that I was trying to serialize the result of the `map` function. I fixed the same error as above by wrapping the `map` function into the `list` function. – Davide Calarco May 26 '22 at 07:57
  • Thank you for your hint! I came into this error in writing fastAPI application, after checking here and there, I discovered it's because I wrapped a numpy array in the return object. Just convert this numpy array to a normal list solved this problem. – Seraph Mar 27 '23 at 08:15
7

One of the fast ways to create a dict from equal-length tuples:

>>> t1 = (a,b,c,d)
>>> t2 = (1,2,3,4)
>>> dict(zip(t1, t2))
{'a':1, 'b':2, 'c':3, 'd':4, }
Abhishake Gupta
  • 2,939
  • 1
  • 25
  • 34
1

I got dictionary update sequence element #0 has length 3; 2 is required
When I was trying to convert a dict to a list using .values()

Solved it by using .items()

list(dict(new_row.items()))
Herker
  • 552
  • 6
  • 20
0

To anyone following patrick collins video[1] on smart contract development who runs into this error code; in the brownie-config.yaml file I had an '=' instead of a '-' in the following line(s)

...
compiler:
  solc:
    remappings:
      = '@openzeppelin=OpenZeppelin/openzeppelin-        contracts@4.2.0'

The first '=' should be '-'. The second one is as it should be.

reference: [1]: https://www.youtube.com/watch?v=M576WGiDBdQ

wayne
  • 11
  • 2