I am returning 5
for my computational field old_default_code
, and I am getting the following error:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
What am I doing wrong?
Python code of the function:
def _old_default_code(self, cr, uid, ids, name, arg, context=None):return '5'_columns = {'old_default_code' : fields.function(_old_default_code, type='char', size=32, method=True, store=False, multi=False) }
XML code:
<?xml version="1.0" encoding="utf-8"?><openerp><data><!-- mrp_bom --><record id="adamson_mrp_bom_form_view" model="ir.ui.view"><field name="name">adamson.mrp.bom.form.view</field><field name="model">mrp.bom</field><field name="type">form</field><field name="inherit_id" ref="mrp.mrp_bom_form_view" /><field name="arch" type="xml"><xpath expr="//notebook/page[@string='Components']/field/tree[@string='Components']/field[@name='sequence']" position="before" ><field name="old_default_code" /><button class="oe_inline oe_stat_button" type="object" string="Go!" icon="gtk-go-forward" name="action_go" attrs="{'invisible':[('old_default_code','=', '5')]}" /></xpath>
Best Answer
@Nebojsa
The field.function always expect a dictionary to be returned and in your case you are just return an "Integer". The default behaviour of the system is that it expects a dictionary where the key is the "id" of the record and value is the value you want to return.
For example:
If you want to return '5' in your case and the record id is 2 then following will be the dictionary {2:5}
Note:
While doing calculation whatever ids you have got in your method for all those ids you should return a value in the dictionary, even if no value can be found/calculated then you should return at least false against those ids, but make sure you at least return some value against all the ids that you have got in your function.
Let me know if you find trouble in this
Hope this helps..
When encountering the 'valueerror: dictionary update sequence element' error in Python, it usually means that you are trying to update a dictionary with an iterable object that contains elements that cannot be inserted into a dictionary.
To resolve this error, you can convert the iterable object into a dictionary before updating it. Alternatively, you can modify the iterable object to ensure that it only contains elements that can be inserted into a dictionary.
It is recommended to carefully review the code that leads to this error and make the necessary changes to ensure compatibility with dictionary updates.
By following these steps, you should be able to fix the 'valueerror: dictionary update sequence element' error and continue with the execution of your Python program smoothly.
The most common way to come across this error is when you have a type casting error, for example,
MWE to regenerate the error -
str_var = 'abc'str_var = dict(str_var)
Running this gives the errorValueError: dictionary update sequence element #0 has length 1; 2 is required
Hope this helps.