Creating form buttons and actions

Hi guys,

In this tutorial I will learn you how to create buttons in views. The form buttons are always shown at the top of the form and are very handy to perform actions on your current record. After this tutorial you can create your own buttons and actions behind the buttons.

1. Creating your model and fields

The first step that you need to do is to create a model and add the fields you need. In my example I will just create a simple form with two fields named ‘name’ and ‘password’. So my model looks like this:

This creates a new model in the database named ‘button.demo’ and will contain two custom added fields: ‘name’ and ‘password’. The field ‘name’ will be a required field and by default it will contain the text ‘Click on generate name!’)

2. Creating the view with buttons

After the model fields have been made it is time to create your view. For this example I will create a simple form view that shows these two fields and I will add three buttons:

Which will look like this:
Form view example
So, how exactly does Odoo know that the buttons need to be at the top of the form? Because of the header tag! Everything within this tag will be shown at the top of your form. So, this creates three buttons, but how do we trigger an action from this button? Have a closer look at the code for one of those buttons:

Noticing anything? The type=”object” is telling us that we have an object and the name=”generate_record_name” is a link to a Python function. When the user clicks on the button “Generate name” Odoo will trigger the Python function ‘generate_record_name’ and from there on we can do whatever we want. So let us create the actions for the buttons!

3. Creating actions behind the buttons

Now that the view is built and we gave all buttons a name that makes the Python go off we should create the Python functions! Lets first create one for the ‘generate_record_name’ button:

self is defining the current record, so this means we can access anything from that record and modify it. The self.write will update the values for this record in the database. By saying ‘name’: we are telling to update the field ‘name’ in the view. In this example I created a simple function that generates a random name between 9 and 15 characters long.
Tip: @api.one is used for one record and cannot be used for multiple records. If you’d want this you would need @api.multi.
If you would now click on the button a name would be generated from this function and would be shown directly on the view.

Now create the second function ‘generate_record_password’ for the button “Generate password”:

This function does almost the same as the previous but it will generate a password between 12 and 15 characters and write it on the field ‘password’ in place of ‘name’.

Next, lets create our third function ‘clear_record_data’ for the button ‘Clear data’. This function will clear all data from the fields:

That is all! You can now create buttons with actions and perform changes on the fields.

4. Conclusion

Buttons are very handy in Odoo and can do just about anything. It is easy to perform actions for the user, change fields, show new views, …
Tip: You can also use buttons in combination with attrs and flows to make them even more useful! You can find this tutorial here.

Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account. Has this tutorial helped you, do you have any feedback or questions? Post away!




Odoo Experts