如何使用Openerp / Odoo中的自定义按钮保存表单数据

时间:2016-04-05 13:52:46

标签: forms button openerp odoo-8

我们有一个带字段的表单。我们添加了自己的“保存”按钮,并希望在单击此按钮时将表单上的数据保留到服务器。

我们知道如何在服务器上创建操作来处理按钮点击,但不知道如何检索表单数据。

目前,我们正在使用内置的“保存”按钮,但需要触发一些额外的功能,因此请求。

这就是我们目前的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>

1 个答案:

答案 0 :(得分:3)

这里有一个可以帮助你的小例子。首先,您需要向按钮添加一些模型操作,如下所示:

<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之前保存在db中。 希望这会对你有所帮助。