Search code examples
pythontryton

ValueError: not enough values to unpack (expected 1, got 0)


I coded a field(a) on classA which is to automatically take the contents of another field(b) in another classB
After updating my own developing on a module I tried to fill in a form on tryton, then I tried to save the form.

But there was an error

Traceback (most recent call last):
  File "/trytond/wsgi.py", line 104, in dispatch_request
    return endpoint(request, **request.view_args)
  File "/trytond/protocols/dispatcher.py", line 48, in rpc
    request, database_name, *request.rpc_params)
  File "/trytond/wsgi.py", line 72, in auth_required
    return wrapped(*args, **kwargs)
  File "/trytond/protocols/wrappers.py", line 131, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/trytond/protocols/dispatcher.py", line 197, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/trytond/model/modelsql.py", line 832, in read
    getter_results = field.get(ids, cls, field_list, values=result)
  File "/trytond/model/fields/function.py", line 106, in get
    return dict((name, call(name)) for name in names)
  File "/trytond/model/fields/function.py", line 106, in <genexpr>
    return dict((name, call(name)) for name in names)
  File "/trytond/model/fields/function.py", line 101, in call
    return dict((r.id, method(r, name)) for r in records)
  File "/trytond/model/fields/function.py", line 101, in <genexpr>
    return dict((r.id, method(r, name)) for r in records)
  File "/trytond/modules/module_designing/design.py", line 15702, in On_change_design
    ('Description', '=', self.id),
ValueError: not enough values to unpack (expected 1, got 0)

, the method mentioned on the error is this : (this method I used it on my field(b) on a class B to call another field(a) on another class A)

 def On_change_design(self,Name):
          Design = Pool().get('design.classA')
          design, = Design.search([
                 ('classB', '=', self.id),
                   ])
          return design.id

field(b) = fields.Function(fields.Many2One('design.classA', 'test'), 'On_change_design')

the field(b) which will take the contain of the field(a)
this is how I was coded the field(a):

field(a) = fields.Function(fields.Char('area '),'on_change_parameters')                                                                              

Any help will be appreciated, I want to know what's wrong and what I should do.
Or can anyone help me and tell how I can code the method onchange to make the field(b) take automatically the contents of another field(a) from another class(a)


Solution

  • Function fields are computed after you save. On your function you are performing a search into a related table and unpacking the result. This has no problem when the search returns a single record but in your case the search does not return any record so this makes the code crash.

    You should use a safer code, that tests if the serach return any result before unpacking. Something like this:

    
    def on_change_design(self,Name):
        Design = Pool().get('design.classA')
        designs = Design.search([
            ('classB', '=', self.id),
            ], limit=1)
        if designs:
            design, = designs
            return design.id
        return None
    

    Note that I also added a limit on the search to ensure a maximum of one record is returned. This will also prevent to crash when multiple records are returned but you may want a diferent behaviour. I also added a explicit None return to make it clar that function will return None when no search is found.