Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
15986 Widoki

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)               

                        })

  

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
sty 22
14724
0
sie 16
4841
1
maj 19
5684
2
gru 18
9185
0
mar 15
4479