在 Odoo 中创建多步向导(wizard)的步骤
1. 定义视图
在视图文件(如 `wizard.xml`)中,定义一个包含多个步骤的表单视图。每个步骤可以使用不同的表单布局。
<record id="wizard_with_step_form" model="ir.ui.view">
<field name="name">wizard_with_step.form</field>
<field name="model">wizard_with_step</field>
<field name="arch" type="xml">
<form string="Wizard with Steps">
<group>
<group>
<field name="state" invisible="1"/>
<div id="step1" attrs="{'invisible': [('state', '!=', 'step1')]}" class="oe_form_box">
<group>
<field name="name1"/>
<button name="action_next" type="object" string="Next" class="btn-primary"/>
</group>
</div>
<div id="step2" attrs="{'invisible': [('state', '!=', 'step2')]}" class="oe_form_box">
<group>
<field name="name2"/>
<button name="action_previous" type="object" string="Previous"/>
<button name="action_next" type="object" string="Finish" class="btn-primary"/>
</group>
</div>
</group>
</group>
</form>
</field>
</record>
2. 定义模型
在模型文件(如 `wizard.py`)中,定义一个继承自 `TransientModel` 的类,并添加状态字段和相应的字段。
from odoo import models, fields
class WizardWithStep(models.TransientModel):
_name = 'wizard_with_step'
_description = 'Wizard with Steps'
name1 = fields.Char('Name 1')
name2 = fields.Char('Name 2')
state = fields.Selection([('step1', 'Step 1'), ('step2', 'Step 2')], default='step1')
3. 定义方法
在模型中定义方法来处理按钮点击事件,更新状态并返回相应的视图。
def action_next(self):
# 处理下一步的逻辑
if self.state == 'step1':
self.write({'state': 'step2'})
return {
'type': 'ir.actions.act_window',
'res_model': 'wizard_with_step',
'view_mode': 'form',
'view_type': 'form',
'res_id': self.id,
'views': [(False, 'form')],
'target': 'new',
}
def action_previous(self):
# 处理上一步的逻辑
if self.state == 'step2':
self.write({'state': 'step1'})
return {
'type': 'ir.actions.act_window',
'res_model': 'wizard_with_step',
'view_mode': 'form',
'view_type': 'form',
'res_id': self.id,
'views': [(False, 'form')],
'target': 'new',
}
4. 注册视图
在模块的 `__init__.py` 和 `__openerp__.py` 文件中注册视图。
`__init__.py`
from . import wizard
`__openerp__.py`
{
'name': 'Wizard with Steps',
'version': '1.0',
'depends': ['base'],
'data': [
'wizard.xml',
],
'installable': True,
'application': True,
}
5. 测试向导
在 Odoo 中打开向导,测试多步流程是否按预期工作。
通过以上步骤,您可以创建一个包含多个步骤的向导,用户可以逐步完成复杂操作。