frostymarvelous
frostymarvelous

Reputation: 2805

How to save form data using a custom button in Openerp/Odoo

We have a form with fields. We have added our own "Save" button, and want to persist the data on the form to the server when this button is clicked.

We know how to create actions on the server to handle button clicks, but no idea how to retrieve the form data.

Currently, we are using the inbuilt Save button but need to trigger some extra functionality hence the request.

This is what our XML looks like currently.

    <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>

Upvotes: 3

Views: 5007

Answers (1)

Danila Ganchar
Danila Ganchar

Reputation: 11223

Here a small example which can help you. First of all you need to add some action of model to button like this:

<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>

After this you need to add method to your model:

# 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'}

Restart openerp-server and update your module.

Be careful! Object will be saved in db before your action_my_action. Hope this helps you.

Upvotes: 3

Related Questions