Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
15959 Tampilan

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
Buang
Jawaban Terbai

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
Buang
Post Terkait Replies Tampilan Aktivitas
1
Jan 22
14682
0
Agu 16
4797
1
Mei 19
5655
2
Des 18
9169
0
Mar 15
4460