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

And*_*ius 4 python openerp openerp-7

假设我有这样的课程:

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'),
    }
Run Code Online (Sandbox Code Playgroud)

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

例如,假设我调用create这样(此方法在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
Run Code Online (Sandbox Code Playgroud)

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

但这只适用于写方法.

Bha*_*dra 5

试试这个值,如果second.class objpartnerone2many创建.

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)
Run Code Online (Sandbox Code Playgroud)