Openerp/Odoo 如何使用自定义按钮保存表单数据
How to save form data using a custom button in Openerp/Odoo
我们有一个带有字段的表单。我们已经添加了自己的 "Save" 按钮,并希望在单击此按钮时将表单上的数据持久保存到服务器。
我们知道如何在服务器上创建操作来处理按钮点击,但不知道如何检索表单数据。
目前,我们正在使用内置的“保存”按钮,但需要触发一些额外的功能,因此提出了请求。
这就是我们 XML 目前的样子。
<record model="ir.ui.view" id="petra_ticket_hold_dialog">
<field name="name">petra.ticket_request.hold.dialog</field>
<field name="model">petra.ticket_request</field>
<field name="arch" type="xml">
<form string="Hold Ticket" edit="false" create="false" delete="false">
<sheet>
<group colspan="2">
<field name="hold_reason"/>
<field name="status" invisible="1"/>
</group>
<button string="Save" />
</sheet>
</form>
</field>
</record>
这里有一个小例子,可以帮助你。首先,您需要像这样向按钮添加一些模型操作:
<record model="ir.ui.view" id="petra_ticket_hold_dialog">
<field name="name">petra.ticket_request.hold.dialog</field>
<field name="model">petra.ticket_request</field>
<field name="arch" type="xml">
<form string="Hold Ticket" edit="false" create="false" delete="false">
<sheet>
<group colspan="2">
<field name="hold_reason"/>
<field name="status" invisible="1"/>
</group>
<!-- it means that will be calls method 'action_my_action' of object 'petra.ticket_request' -->
<button string="Save" name="action_my_action" type="object"/>
</sheet>
</form>
</field>
</record>
在此之后,您需要向模型添加方法:
# you can use @api.multi for collection processing like this:
# for ticket in self: ...something do here
# or you can use @api.model for processing only one object
@api.model
def action_my_action(self):
# here you have values from form and context
print(self.hold_reason, self._context)
# todo something here... and close dialog
return {'type': 'ir.actions.act_window_close'}
重新启动 openerp-server 并更新您的模块。
小心!对象将保存在您 action_my_action
之前的数据库中。
希望对你有帮助。
我们有一个带有字段的表单。我们已经添加了自己的 "Save" 按钮,并希望在单击此按钮时将表单上的数据持久保存到服务器。
我们知道如何在服务器上创建操作来处理按钮点击,但不知道如何检索表单数据。
目前,我们正在使用内置的“保存”按钮,但需要触发一些额外的功能,因此提出了请求。
这就是我们 XML 目前的样子。
<record model="ir.ui.view" id="petra_ticket_hold_dialog">
<field name="name">petra.ticket_request.hold.dialog</field>
<field name="model">petra.ticket_request</field>
<field name="arch" type="xml">
<form string="Hold Ticket" edit="false" create="false" delete="false">
<sheet>
<group colspan="2">
<field name="hold_reason"/>
<field name="status" invisible="1"/>
</group>
<button string="Save" />
</sheet>
</form>
</field>
</record>
这里有一个小例子,可以帮助你。首先,您需要像这样向按钮添加一些模型操作:
<record model="ir.ui.view" id="petra_ticket_hold_dialog">
<field name="name">petra.ticket_request.hold.dialog</field>
<field name="model">petra.ticket_request</field>
<field name="arch" type="xml">
<form string="Hold Ticket" edit="false" create="false" delete="false">
<sheet>
<group colspan="2">
<field name="hold_reason"/>
<field name="status" invisible="1"/>
</group>
<!-- it means that will be calls method 'action_my_action' of object 'petra.ticket_request' -->
<button string="Save" name="action_my_action" type="object"/>
</sheet>
</form>
</field>
</record>
在此之后,您需要向模型添加方法:
# you can use @api.multi for collection processing like this:
# for ticket in self: ...something do here
# or you can use @api.model for processing only one object
@api.model
def action_my_action(self):
# here you have values from form and context
print(self.hold_reason, self._context)
# todo something here... and close dialog
return {'type': 'ir.actions.act_window_close'}
重新启动 openerp-server 并更新您的模块。
小心!对象将保存在您 action_my_action
之前的数据库中。
希望对你有帮助。