gpt4 book ai didi

python - 如何创建添加 one2many 值的记录?

转载 作者:太空狗 更新时间:2023-10-30 02:04:14 25 4
gpt4 key购买 nike

假设我有这样的类(class):

class First(orm.Model):
_name = 'first.class'
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner'),
'res_ids': fields.one2many('second.class', 'first_id', 'Resources'),
}
class Second(orm.Model):
_name = 'second.class'
_columns = {
'partner_id': fields.many2one('res.partner', 'Partner'),
'first_id': fields.many2one('first.class', 'First'),
}

现在我想写一个方法,当它被调用时,它会创建 First 类记录,从 Second 类中获取值。但是我不明白在创建 First 类时我应该如何在 one2many 字段中传递值。

例如,假设我这样调用创建(此方法在 Second 类中):

def some_method(cr, uid, ids, context=None):
vals = {}
for obj in self.browse(cr, uid, ids):
#many2one value is easy. I just link one with another like this
vals['partner_id'] = obj.partner_id and obj.partner_id.id or False
#Now the question how to insert values for `res_ids`?
#`res_ids` should take `id` from `second.class`, but how?..
vals['res_ids'] = ??
self.pool.get('first.class').create(cr, uid, vals)
return True

这里有一个关于插入 one2many 值的文档: https://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html/#osv.osv.osv.write

但这仅适用于 write 方法。

最佳答案

试试这个值,如果 second.class objpartner 而不是 one2many 被创建。

obj = self.browse(cr, uid, ids)[0]

if obj.partner_id:
vals{
'partner_id': obj.partner_id.id,
'res_ids': [(0,0, {
'partner_id': obj.partner_id.id, #give id of partner
'first_id': obj.first_id.id #give id of first
})]
}
self.pool.get('first.class').create(cr, uid, vals)

关于python - 如何创建添加 one2many 值的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22787243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com