如何基于另一个字段创建动态序列?

2024-10-03 02:36:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想基于category_id字段为产品创建一个动态序列。我在product.category中添加了一个category_code。但是一旦我在create函数中调用它,它就会返回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

Tags: selfiddefault产品createcode动态序列
1条回答
网友
1楼 · 发布于 2024-10-03 02:36:49

试着这样做:

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

相关问题 更多 >