Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
15960 Vistas

I have a situation where I want to push(insert) my data to other model

Eg:

class sales_quotation(models.Model):

     _name = 'tbl.sales.quotation'

     batch_ids = fields.Many2many('batch.master',string= 'Batches')

     product_id = fields.Many2one('product.product',string= 'Product')

    @api.one
    @api.multi
    def send_to_order(self):
        cr = self.env.cr
        uid = self.env.uid
        ids = self.ids
        context = 'context'        
        for temp in self:

             sales_order = self.pool.get('tbl.sales.order').create(cr,uid,{

                      'product_id':temp.product_id.id,

                        'batch_ids':--------------------------------?(What i have to write here i am totally confused i cant figure it out)               

                        })

  

Avatar
Descartar
Mejor respuesta

You are combining the old and new api, just stick with the new one. In the new API, you don't need cr, uid & ids, you can achieve most things via self.env. So your send_to_order function could be greatly simplified to:

    @api.multi
    def send_to_order(self):
        for temp in self:

             sales_order = self.env['tbl.sales.order'].create({

                      'product_id': temp.product_id.id,

                      'batch_ids': [(6, None, self.batch_ids.ids)]

                    })

Things I have changed:

removed @api.one, it has been deprecated as it was confusing to use and was often used wrongly

creation of new recordsets can be done via the new self.env['model_name'] syntax, instead of the old pool.get('model_name')

And the part you were actually asking for, how to write the many2many field batch_ids. This is using Odoo's tuple syntax (the details can be found here: https://www.odoo.com/documentation/10.0/reference/orm.html#odoo.models.Model.write) . Basically, you have to somehow pass recordsets in the return, which is converted to json, so you can't pass records directly. Instead you give Odoo a command and a set of ids. In this case the 6 means: set this Many2many field to the records with the ids given in the last tuple element (the middle element is ignored in this case, but it must be present).

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
ene 22
14685
0
ago 16
4797
1
may 19
5656
2
dic 18
9170
0
mar 15
4460