1

So I have a Sale model and SaleLine model. Sale model have a field sale_line_ids as One2many from SaleLine model.

Sale

class Sale(models.Model):
    _name = 'store.sale'
    _description = 'Store Sale'

    ...
    sale_line_ids = fields.One2many('store.sale_line', 'sale_id', string='Sale Lines')
    ...

Sale Line

class SaleLine(models.Model):
    _name = 'store.sale_line'
    _description = 'Store Sale Line'

    sale_id = fields.Many2one('store.sale', string='Sale Reference', ondelete='cascade', index=True)
    product_id = fields.Many2one('store.product', string='Product', required=True)
    quantity = fields.Integer(string='Quantity', required=True)

I want to create SaleLine model programmatically and add that model to sale_line_ids field. How can I do that?

NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30
  • It will be difficult. Are you sure you want to do that programmatically ? Is it an [XY problem](https://en.wikipedia.org/wiki/XY_problem) ? – Lenormju Jan 24 '22 at 14:32
  • Yes I know, because I can't do that with UI. This is just an example code. – NM Naufaldo Jan 24 '22 at 14:39
  • I think you could do that using a intermediate model, like an `TransientModel` model, whre you relate the parent `Sale` with the `SaleLine` childs, then override the `create` method or in a button of `TransientModel` model call `SaleLine` `create` method to asigne the proper `sale_id` to the childs. Thats what I understand for 'programmatically'. – Juan Salcedo Jan 24 '22 at 23:14

1 Answers1

0

This answer is what I actually want to implement. However, the model is immediately saved to a database. I need to create a SaleLine model using env[].create({}) method.

self.env['store.sale_line'].create({
    'sale_id': rec.id,
    'product_id': id_from_another_model,
    'quantity': quantity_from_another_model,
})

After that, I need to commit in order to save the data in a database.

self.env.cr.commit()

UPDATE

The previous answer required record to store directly. The best answer to solve the problem is to create temporary record that only saved when user click the save button.

Syntax

(0, 0,  { values })

First create sale_line list

sale_line = []
for data in list_of_data:
    sale_line.append([0, 0, {
        'sale_id': data['sale_id'],
        'product_id': data['product_id'],
        'quantity': data['quantity'],
    }])

Assign sale_line list to sale_line_ids field

self.write({'sale_line_ids': sale_line})

And override the create method to commit the change

@api.model
def create(self, values):
    self.env.cr.commit() # This is the important part

    return super(Sale, self).create(values)
NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30