- 上传者: Administrator
- 上传时间:2023年11月19日 02时00分51秒
* Wizard * 向导* *** Odoo wizards are used to create interactive sections with users. Odoo向导用于创建与用户交互的部分。 Such interactive sections are extremely benef ......
Wizard
向导
Odoo wizards are used to create interactive sections with users.
Odoo向导用于创建与用户交互的部分。
Such interactive sections are extremely beneficial to any business flow.
这样的交互式部分对任何业务流程都非常有益。
Transient and Abstract models are the most commonly used model classes in Odoo for wizard creation. However, the most common mode is the transient mode. This provides all of the characteristics of a model class except that the data in a Transient model is deleted on a regular basis, hence the name Transient. Wizards are commonly used to perform database persistent model operations.
Let's look at an example of a use case. Consider a model student.student for managing all of an educational organization's student records
from odoo import fields, models, api
class Student(models.Model):
_name = "student.student"
_description = "Student"
name = fields.Char(string="Name", required=True)
partner_id = fields.Many2one('res.partner', string="Partner")
phone = fields.Char(string="Phone Number")
email = fields.Char(string="Email", required=True)
status = fields.Char(string="Status")
leave_ids = fields.One2many('student.leave', 'student_id', string="Leaves")
For recording all of the student's leaves taken, there is an one2many type field. Let's make the model student.leave the required fields.
class StudentLeave(models.Model):
_name = "student.leave"
_description = "Student Leave"
student_id = fields.Many2one('student.student', string="Student")
date = fields.Date(string="Date")
reason = fields.Char(string="Reason")
The wizard model will then be developed, allowing the user to select a date and add a reason for leave. For this wizard model, we use the transient model class.
from odoo import api, fields, models
class StudentLeaveWizard(models.TransientModel):
_name = 'student.leave.wizard'
_description = 'Student Leave'
student_id = fields.Many2one('student.student', string="Student",
readonly=True)
date = fields.Date(string="Date")
reason = fields.Char(string="Reason")
Add a button for creating a leave record inside the student form view.
<header >
<button name="create_leave" string="Leave" class="oe_highlight"
type="object"/>
</header>
In the student model, write a Python function for the button action. Let us return a widget where the user can enter the date and reason for leaving. Create a new leave record for the student using the widget values. Create a new record in the widget model with the student_id field set to the current student. Then, in the button click, return that object.
def create_leave(self):
wizard = self.env['student.leave.wizard'].create({
'student_id': self.id
})
return {
'name': _('Student Leave'),
'type': 'ir.actions.act_window',
'res_model': 'student.leave.wizard',
'view_mode': 'form',
'res_id': wizard.id,
'target': 'new'
}
The next step is to create a form view for the wizard that includes buttons for performing actions on the record. For the wizard view, two buttons named CREATE and CANCEL are added, each with the functionality indicated by the name.
<record model="ir.ui.view" id="leave_wizard_form_view">
<field name="name">Leave</field>
<field name="model">student.leave.wizard</field>
<field name="arch" type="xml">
<form string="Leave">
<sheet>
<group>
<group>
<field name="student_id"/>
</group>
<group>
<field name="date" required="1"/>
</group>
</group>
<separator string="Reason"/>
<field name="reason" required="1" nolabel="1" placeholder="Give leave reason"/>
</sheet>
<footer>
<button type="object" name="create_leave_from_wizard" class="btn btn-primary"
string="CREATE"/>
<button string="CANCEL" special="cancel"/>
</footer>
</form>
</field>
</record>
On button click, this will return the widget form.
Now inside the create_leave_from_wizard method, it will perform the required action. Inside the wizard mode, this method has to be written. Create a new record in the model student.leave using the widget value on the button click.
def create_leave_from_wizard(self):
self.env['student.leave'].create({
'student_id': self.student_id.id,
'date': self.date,
'reason': self.reason
})
Upgrade the module after making all the necessary changes, then verify that the student's new leave record was successfully created.
This is how a wizard is made, which enhances the user experience in general.
本文章从网上收集,如有侵权请联系tderp@tderp.com删除
- 微信扫一扫,一分也是爱:
服务原则及地区范围
宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。
我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!
通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。
宜兴地区提供上门服务:
- 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
- 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。