Python 值错误:字典更新序列元素 #0 的长度为 1;2 是必需的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27628039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: dictionary update sequence element #0 has length 1; 2 is required
提问by Nebojsa
I am returning 5for my computational field old_default_code, and I am getting the following error:
我正在返回5我的计算领域old_default_code,但出现以下错误:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
值错误:字典更新序列元素 #0 的长度为 1;2 是必需的
What am I doing wrong?
我究竟做错了什么?
Python code of the function:
函数的Python代码:
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 代码:
<?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>
采纳答案by Hardik Patadia
@Nebojsa
@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.
field.function 总是期望返回一个字典,在你的情况下你只是返回一个“整数”。系统的默认行为是它需要一个字典,其中键是记录的“id”,值是您要返回的值。
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}
如果您想在您的案例中返回 '5' 并且记录 ID 为 2,那么以下将是字典 {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.
在为所有这些 id 计算您在方法中获得的任何 id 时,您应该在字典中返回一个值,即使找不到/计算出任何值,您也应该针对这些 id 至少返回 false,但请确保您至少针对您在函数中获得的所有 id 返回一些值。
Let me know if you find trouble in this
如果您在这方面遇到问题,请告诉我
Hope this helps..
希望这可以帮助..
回答by wootman
The most common way to come across this error is when you have a type casting error, for example,
遇到此错误的最常见方法是当您遇到类型转换错误时,例如,
MWE to regenerate the error -
MWE 重新生成错误 -
str_var = 'abc'
str_var = dict(str_var)
Running this gives the error
ValueError: dictionary update sequence element #0 has length 1; 2 is required
运行这个会给出错误
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Hope this helps.
希望这可以帮助。

