通过将值分配给many2many字段来创建块对象

时间:2017-08-24 16:11:59

标签: python openerp many-to-many odoo-8 openerp-8

我尝试创建一个功能,通过向导创建大量实例,为多个字段分配值,但在测试时我收到错误:

Singleton expected: model3(167, 168, 169, 170, 171, 172, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 188, 187, 188, 190, 191, 193, 194, 195, 1978, 1978, 2007, 1920, 202, 203, 204, 20, 20, 20, 20, 20, 20, 20, 20, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246)

我留下代码以防你可以帮助我。

class Modelo1Model(models.Model):
    _name = 'modelo1'

    mod1_modelo3_ids = fields.Many2many('modelo3')

class Modelo2Model(models.Model):
    _name = 'modelo2'

    mod2_modelo3_ids = fields.Many2many('modelo3')

class Modelo3Model(models.Model):
    _name = 'modelo3'

    #...

class Modelo1Wizard(models.TransientModel):
    _name = 'modelo1.wizard'

    def _default_i(self):
        return self.env['modelo1'].browse(self._context.get('active_ids'))

    modelo1_ids = fields.Many2many(
        'modelo1',
        default = _default_i)

    @api.multi
    def create_modelo2(self):
        for i in self.modelo1_ids:
            registros_many2many = i.mod2_modelo3_ids.ids
            i.ensure_one()
            modelo2_obj = self.env['modelo2']
            modelo2_vals = {
                'mod2_modelo3_ids': registros_many2many
                 }
            modelo2_create_id = modelo2_obj.create(modelo2_vals)

2 个答案:

答案 0 :(得分:0)

@api.multi

当你使用这个装饰器装饰你的方法时,你暗示self将是一个记录集。为了正确写入您的记录,您必须进行迭代。例如:

@api.multi
    def create_modelo2(self):
        for record in self:
            for i in record.modelo1_ids:

答案 1 :(得分:0)

通过以下this解决,此处显示如何使用字段x2many