Get parent in the create classmethod

Hi everyone. I have a module similar to a sale, in which I have 3 models, one would be sale, the others are saleline and otherlines, I want that when a saleline is created I create a line in otherlines with the same product, where otherlines is a “set” of the products of salelines. The problem is when I edit the create method of saleline I don’t have how to make reference to the sale, this is None. The code is the following:

class Sale(ModelSQL, ModelView):
    __name__ = 'sale.sale'
    lines = fields.One2Many('sale.line', 'sale', 'Lines')
    otherlines = fields.One2Many('sale.otherline', 'sale', 'Other Lines', readonly=True)
class SaleLine(sequence_ordered(), ModelSQL, ModelView):
    __name__ = 'sale.line'
   sale = fields.Many2One('sale.sale', 'Sale', ondelete='CASCADE', select=True, required=True,)
   @classmethod
   def create(cls, vlist):
        pool = Pool()
        SaleOtherLine = pool.get('sale.otherline')
        for values in vlist:
            if values.get('product'):
                other_line = SaleOtherLine()
                other_line.product = values['product']
                other_line.sale = values['sale'] # this value is None
                other_line.save()
        return super(SaleLine, cls).create(vlist)
class SaleOtherLine(ModelSQL, ModelView):
    __name__ = 'sale.otherline'
    sale = fields.Many2One("sale.sale", string = "Sale")
    product = fields.Many2One("product.product", string = 'Product')

Thanks in advance.

You need to ovierride the create method to create the related records.

As create returns the created records, you can always create the other lines after the creation of the lines. Something like:

   @classmethod
   def create(cls, vlist):
        lines =  super().create(vlist)
        for line in lines: 
             # Compute your new records and save them
        return lines

Hope it helps!

Thank you very much! it helped me a lot!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.