This question has been flagged
1 Reply
16116 Views

Hello guys!

We are in Odoo 10. I have created a button on each sale order line. We want to duplicate the sale order line in the same sale order when the button is clicked. See image below, the new duplicate icon at the right.

I think that we have to retrieve all the values of the current clicked line and after create the new line.

My skills are too limited to achieve this kind of thing.

COuld you guide me just a little please?

This is not really what we want to achieve, but this would give me a great beginning to achieve the rest.


Our actual code (empty!!!)

def dup_line(self):
        _logger.error("dup_line BEGIN")
        _logger.error("    self :: %s", str(self))
       
        return True


In image :



EDIT #1

I have now this code for my button :

@api.one
def dup_line(self):
     self.copy()

But I get this error :

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it- creation/update: a mandatory field is not correctly set
[object with reference: order_id - order.id]


EDIT #2

It is clear that the problem is the order_id field.

With this code, I can easily see the self.order_id in the debug. But the copy() function doesn't include the order_id field.

@api.one
def dup_line(self):
    logger.error("    self.order_id :: %s", str(self.order_id))
    self.copy()





Avatar
Discard
Best Answer

Hi,

You may try like this:

class SaleOrderLine(models.Model):
    _inherit = "sale.order.line"
     

    @api.multi
    def dup_line(self):
         self.copy(default={'order_id':self.order_id.id})

If you want to pass some values to the newly created record, you can pass on the default arguement of copy()

order_id is not copied by default because copy=False attribute set on the field definition, if you check in sale.order.line model.

order_id = fields.Many2one('sale.order', string='Order Reference', required=True, ondelete='cascade', index=True, copy=False)
Avatar
Discard
Author

Thanks for your answer. COuld you explain why the order_id is not already copied with other values? Why do you we have to give it to copy() function?

Author

Your line works well. Thanks for this. But, I have to refresh the sale order to see the new line. Not a problem for the moment.

check updated answer for why its not copied by default

Author

I learn a lot with your answers! Big thanks!

Thanks @ Akhil P.

+1