Skip to Content
Menu
This question has been flagged
1 Reply
14795 Views

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
Discard
Best Answer

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
Discard
Related Posts Replies Views Activity
1
Jan 22
13558
0
Aug 16
3971
1
May 19
4504
2
Dec 18
7641
0
Mar 15
3605