Dynamic record stages

Stages typically indicate the current status of a record and are usually fixed and not intended for direct modification. To introduce flexibility, a many2one field can be used, allowing users to select, change, or even create new stage values as needed. This method is effectively demonstrated through practical examples, showcasing its application and benefits.

To manage stages independently, we will create a separate model.

from odoo import models, fields

class DynamicStage(models.Model):
   _name = "dynamic.stage"
   _description = "Dynamic Stage"
   _rec_name = "name"

   name = fields.Char(string="Stage Name", required=True)
   sequence = fields.Integer(string="Sequence", default=10)
   fold = fields.Boolean(string="Folded in Kanban", default=False)
                        

This model should be integrated into the main parent model using a many2one relationship.

                        @api.model
def _get_stage_id(self):
   stage = self.env['dynamic.stages'].search([], limit=1)
   return stage.id if stage else False

stage_id = fields.Many2one(
   'dynamic.stages',
   string="Dynamic State",
   default=_get_stage_id,
   readonly=True,
   required=True,
   help="Represents the current stage of the record"
)
                        

The field can now be added to the view as shown below:

                        <header>
    <field name="stage_id" widget="statusbar" options="{'clickable': '1', 'fold_field': 'fold'}"/ >
</header>

After defining the fields appropriately, the stages become editable and manageable, allowing users to customize and reorganize stage definitions as required.

whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message