在开始状态表单上写入名称序列 - Odoo v8

时间:2017-11-18 19:28:43

标签: python openerp odoo-8

我有这个方法:

class bsi_production_order(models.Model):
    _name = 'bsi.production.order'

    @api.model
    def create(self, vals):
        if vals.get('name', 'New') == 'New':
            if vals.get('production_type') == 'budgeted':
                vals['name'] = 
self.env['ir.sequence'].next_by_code('bsi.production.budgeted') or '/'
            elif vals.get('production_type') == 'nonbudgeted':
                vals['name'] = 
self.env['ir.sequence'].next_by_code('bsi.production.non_budgeted') or '/'
            elif vals.get('production_type') == 'direct':
                vals['name'] = 
self.env['ir.sequence'].next_by_code('bsi.production.direct') or '/'
        return super(bsi_production_order, self).create(vals)
    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Production Date")
    production_type = fields.Selection([
            ('budgeted','Budgeted'),
            ('nonbudgeted','Non Budgeted'),
            ('direct','Direct Order'),
        ], string='Type of Order', index=True, copy=False,
        help=" ")

根据production_type选择创建新记录。

现在,这会在保存后立即写入文档的名称,我有这种状态的方法:

    state = fields.Selection([
        ('draft','Draft'),
        ('confirm','Confirmed'),
        ('inprogress','In progress'),
        ('print_order_inprogress','Print In Progress'),
        ('finished','Finished'),
        ('cancel','Cancel'),
    ], string='State', index=True, copy=False,
    help=" ")
    @api.one
    def prod_start_func(self):
        self.write({'state': 'confirm',})

当我保存文档时,它的名称是在保存时写的,因此默认情况下它处于draft状态。

我需要的是当我实际使用prod_start_func方法将statedraft更改为confirm

时要写的名称

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

只需将代码从create中移除到函数中:

  @api.model
    def create(self, vals):
        if vals.get('name', 'New') == 'New':
            # if you don't want name to equal New.
            vals['name'] = '/'

在你的功能中:

    @api.one
    def prod_start_func(self):
        name = '/'
        if self.production_type == 'budgeted':
                name = self.env['ir.sequence'].next_by_code('bsi.production.budgeted') or '/'
        elif self.production_type == 'nonbudgeted':
                name = self.env['ir.sequence'].next_by_code('bsi.production.non_budgeted') or '/'
        elif self.production_type == 'direct':
                name = self.env['ir.sequence'].next_by_code('bsi.production.direct') or '/'

        self.write({
                'state': 'confirm',
                'name' : name,
                })