pythonodooodoo-10odoo-11

Duplicate record missing many2one field issue


I have my products with extra fields that come from a custom module. I'm trying to rewrite the copy function because otherwise when I try to duplicate the product some values don't duplicate.

So, in this copy function I manage to add those fields but if I try to just add the ids from the original product field, that one loses the values after the product is duplicated. I guess that makes sense.

Code:

class ProductTemplate(models.Model):
    _inherit= 'product.template'

    @api.multi
    def copy(self, default=None):
        default = dict(default or {})
        array_attribute_line_ids = []
        for value_id in self.attribute_line_ids.read([]):
            array_attribute_line_ids += [(4,value_id['id'])]
        print (array_attribute_line_ids)
        default.update({
            # General Information
            # 'standard_price': self.standard_price,

            # Variants > Attributes
            'attribute_line_ids': array_attribute_line_ids,
            # Purchase > Vendors

            # Inventory
            'weight': self.weight,
            'volume': self.volume,

            # Invoicing
            'property_account_income_id': self.property_account_income_id,
            'supplier_taxes_id': self.supplier_taxes_id, #NOT WORKING
            'property_account_expense_id': self.property_account_expense_id,
            'property_account_creditor_price_difference': self.property_account_creditor_price_difference,
        })
        return super(ProductTemplate, self).copy(default)

View: the field at hand is attribute_line_ids shown in the bottom of the image.

enter image description here

Problem: I could create new records based on the original ones but the problem is that one of these values depends on a many2one that needs a value 'product_tmpl_id' which is the id of the product. So, how can I create these value if I don't have the id of the new product yet because it hasn't been duplicated at the moment.

Previously in a different feature I created the record and then using it's id I update these values. Now its not possible.

Is there a way to solve this? Is there a .copy() function that allows me to duplicate all this records without the need of creating then manually?

This is odoo 11 enterprise.


Solution

  • IIUC you want product attributes to be copied when a product is duplicated. I believe you can achieve that by changing the field attribute_line_ids on product.template attribute copy to True:

    from odoo import fields, models
    
    
    class ProductTemplate(models.Model):
        _inherit = 'product.template'
    
        attribute_line_ids = fields.One2many(copy=True)