Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
1 Відповісти
15965 Переглядів

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)               

                        })

  

Аватар
Відмінити
Найкраща відповідь

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).

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
1
січ. 22
14691
0
серп. 16
4800
1
трав. 19
5656
2
груд. 18
9172
0
бер. 15
4462