odoo源码学习之任务中的阶段字段stage_id

 # 案例0004针对form表单

class Task(models.Model):
    _name = "project.task"
    _description = "对于项目中的阶段步骤用many2one字段来展示:一般步骤上selection型字段展示"




    def _get_default_stage_id(self):
        """ Gives default stage_id 该任务获取默认的阶段"""
        project_id = self.env.context.get('default_project_id')
        if not project_id:
            return False
        return self.stage_find(project_id, [('fold', '=', False)])

    @api.model
    def _read_group_stage_ids(self, stages, domain, order):
        """group_expand 更改字段分组行为"""
        search_domain = [('id', 'in', stages.ids)]
        if 'default_project_id' in self.env.context:
            search_domain = ['|', ('project_ids', '=', self.env.context['default_project_id'])] + search_domain
        stage_ids = stages._search(search_domain, order=order, access_rights_uid=SUPERUSER_ID)
        return stages.browse(stage_ids)



    stage_id = fields.Many2one('project.task.type', string='Stage', ondelete='restrict', track_visibility='onchange',
                               index=True,default=_get_default_stage_id, group_expand='_read_group_stage_ids',domain="[('project_ids', '=', project_id)]", copy=False)



    def stage_find(self, section_id, domain=[], order='sequence'):
        """ Override of the base.stage method
            Parameter of the stage search taken from the lead:
            - section_id: if set, stages must belong to this section or
              be a default stage; if not set, stages must be default
              stages
        """
        # collect all section_ids  :搜索所有的阶段的值
        section_ids = []
        if section_id:
            section_ids.append(section_id)
        section_ids.extend(self.mapped('project_id').ids)
        search_domain = []
        if section_ids:
            search_domain = [('|')] * (len(section_ids) - 1)
            for section_id in section_ids:
                search_domain.append(('project_ids', '=', section_id))
        search_domain += list(domain)
        # perform search, return the first found
        return self.env['project.task.type'].search(search_domain, order=order, limit=1).id




前端视图写法

前端用挂件来展示:widget = "statusbar" 阶段可编辑属性值:options = "{'clickable': '1'}"
#xml中展示:< field name = "stage_id" widget = "statusbar" options = "{'clickable': '1', 'fold_field': 'fold'}" / >

#xml中继承视图中添加button:  <xpath expr="//header" position="inside">
# <button name="btn_next_stage" string="next step" type="action" class="oe_highlight oe_read_only"/>
# <button name="btn_last_stage" string="last step" type="action" class="oe_highlight oe_read_only"/>
# </xpath>








原文地址:https://www.cnblogs.com/1314520xh/p/11255867.html