功能:点击创建后返回到mysale.mysale的form视图。
代码:视图文件:
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record model="ir.actions.act_window" id="action_appointment">
<field name="name">创建 appointment</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">create.appointment.wizard</field>
<field name="view_mode">form</field>
<field name="view_id" ref="action_appointment_form"/>
<field name="target">new</field>
</record>
<record model="ir.ui.view" id="action_appointment_form">
<field name="name">mykid_form</field>
<field name="model">create.appointment.wizard</field>
<field name="arch" type="xml">
<form>
<form string="创建一个新预约">
<group>
<field name="product_id"/>
<field name="date_test1"/>
</group>
<footer>
<button name="action_createaption" string="创建" type="object" class="btn-primary"/>
<button string="取消" class="btn-secondary" special="cancel"/>
</footer>
</form>
</form>
</field>
</record>
<menuitem id="menu_1_list3"
name="appointment"
parent="menu_1_list"
action="action_appointment"
sequence="30"/>
</odoo>
模型文件:
from odoo import fields, models, api, _
class CreateAppointmentWizard(models.TransientModel):
_name = "create.appointment.wizard"
_description = "这是一个瞬态模型,信息不会记录到数据库"
product_id = fields.Many2one('product.template', string="产品")
date_test1 = fields.Date(string="日期")
def action_createaption(self):
print("创建成功")
vals = {
'product_id': self.product_id.id,
'date_test1': self.date_test1
}
self.env['mysale.mysale'].create(vals)
return {
'name': _('Appointment'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_id': self.product_id.id,
'res_model': 'mysale.mysale',
'target': 'new',
}
需要注意的是:
1. product_id是一个many2one,拿到后是个元组,所以我们要的值是它的ID
2. res_model 为返回到哪个模型的视图。这里我们返回mysale的模型