如何为不同的任务分配序号?

How to assign sequence number for different task?

我已经为项目创建了模块,当记录保存前缀生成时带有字段前缀 pro-001

当在特定项目下创建任何任务时,将生成 pro-001、pro-002、pro-003

def create(self, cr, uid, vals, context=None):
        project_id = vals.get('project_id')
        project = self.pool.get('project.project').read(cr, uid, project_id, context=context)
        if project and project['prefix']:
           new_id =  self.pool.get('ir.sequence').next_by_id(cr, uid, 'project.task', project['prefix'], context=context)

        return super(projectTask, self).create(cr, uid, vals, context)

对于该要求,我建议为每个项目定义一个序列(sequence_id 见下文)。查看 account.journal 它是如何正确完成的。

然后您可以在 project.task 创作中使用它,例如:

def create(self, cr, uid, vals, context=None):
    project_id = vals.get('project_id')
    project = self.pool.get('project.project').browse(
        cr, uid, project_id, context=context)
    if project and project.sequence_id:
        vals['your_sequence_field_on_task'] = self.pool.get('ir.sequence').next_by_id(
            cr, uid, project.sequence_id.id, context=context)

    return super(projectTask, self).create(cr, uid, vals, context)