OpenERP:创建新记录,一对多关系 [英] OpenERP : create new record ,one2many many2one relationship
问题描述
我已经在A类和其他字段nombre(整数)中创建了on2many字段:
I have created on2many field in class A and other field nombre (integer):
'Inventaire' : fields.one2many('class.b','id_classb'),
'nombre' : fields.integer('Nombre'),
在b类中:
'id_classb' : fields.many2one('class.a', 'ID_classA'),
'ql' : fields.integer('QL'),
我想在a类中创建一个函数,根据nombre字段的值为对象b创建记录.例如,如果nombre = 3,我应该创建3个b类的对象
I want to create a function in class a that create records for object b according to the value of nombre field. for example if nombre =3 I should create 3 object of class b
这是我的功能:
def save_b(self, cr, uid, ids, field_name, arg, context):
a= self.browse(cr, uid, id)
nbr=a.nombre
num=22
for i in range(nbr):
num+=1
self.create(cr, uid, [(0, 0,{'ql':num})])
我得到这些错误: TypeError:range()预期为整数,没有NoneType ValueError:字典更新序列元素#0的长度为3;需要2个
I get these errors : TypeError:range()integer expected ,got NoneType ValueError: dictionary update sequence element #0 has length 3; 2 is required
有人可以帮助我改善我的功能吗?
can someone help me to improve my function?
推荐答案
您遇到以下错误:
-
您的create调用值应该是一个以字段名作为键的字典,而不是包含元组的列表.您正在使用的表示法是用于编写/更新一个或多个字段.
Your create call values should be a dictionary with field names as keys, not a list with tuples. The notation you are using is for writing/updating one2many fields.
您不是在创建"b类"记录,而是在创建"a类"记录(使用self而不是self.pool.get调用)
You are not creating 'class b' records, but creating 'class a' records instead (using self instead of a self.pool.get call)
所以你应该写
def save_b(self, cr, uid, ids, field_name, arg, context):
b_obj = self.pool.get('class.b') # Fixes (#2)
for record in self.browse(cr, uid, ids, context=context):
num = 22
for i in range(record.nombre):
num += 1
new_id = b_obj.create(cr, uid, {
'ql': num,
'id_classb': record.id
}, context=context) # Fixes (#1)
或者作为替代:
def save_b(self, cr, uid, ids, field_name, arg, context):
for record in self.browse(cr, uid, ids, context=context):
sub_lines = []
num = 22
for i in range(record.nombre):
num += 1
sub_lines.append( (0,0,{'q1': num}) )
# Notice how we don't pass id_classb value here,
# it is implicit when we write one2many field
record.write({'Inventaire': sub_lines}, context=context)
备注:在类b中,您到类a的链接在名为'id_classb'的列中? openerp礼节希望您将其命名为"classa_id"或类似名称.
Remark: In class b your link to class a is in column named 'id_classb'? The openerp-etiquette expects you to name them 'classa_id' or something similar.
另外,用大写字母创建列名称的想法也不受欢迎.
Also, creating column names with capitals is frowned upon.
这篇关于OpenERP:创建新记录,一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!