Amy
Amy

Reputation: 70

How to create a Dynamic Sequence based on another field?

I want to create a dynamic sequence for products based on the field category_id. I've added a category_code in product.category. But once I call it inside the create function, it returns False:

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.model
    def create(self, vals):
        if 'default_code' not in vals or vals['default_code'] == '/':
            sequence = self.env.ref('product_sequence.seq_product_auto')
            record=super(ProductProduct, self).create(vals)
            print self.categ_id.category_code
        vals['default_code'] =self.categ_id.category_code + sequence.next_by_id()
        return record

Upvotes: 1

Views: 266

Answers (1)

sfx
sfx

Reputation: 1841

Try like this:

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.model     
    def create(self, vals):
        record=super(ProductProduct, self).create(vals) 
        if 'default_code' not in vals or vals['default_code'] == '/':
            sequence = self.env.ref('product_sequence.seq_product_auto')
            print record.categ_id.category_code
            record.default_code =record.categ_id.category_code + sequence.next_by_id()
        return record

Upvotes: 3

Related Questions