Creating and managing statusbars (selections) in Odoo

Hi guys,

In this tutorial I will learn you how to create and manage statusbars. Statusbars are selections (on database level) which are visually respresented by Odoo. They give you the ability to follow a specific flow and can be very handy to show you the progress that has been made. After this tutorial you can create and manage your own statusbars.

In this example I will create a new form view which contains a statusbar and I’ll add buttons on the form which change the state of the record.

1. Creating the model and fields

The first step is to create a model (if you don’t have one yet) and to add a selection field:

This creates a new model with the name ‘statusbar.demo’ which contains two fields, the field ‘name’ and the field ‘state’. The state field is of the type selection, which means it will contain multiple values which can be chosen. A selection is always built up in the same way, the first part if the database name and the second part is the text that will be shown to the user. For example:

If you wish to change the state to ‘In progress’ you will need to tell the database about ‘progress’, since this is the name the database listens to, while the name shown to the user will be ‘In progress’.
This is all you need on the database level. You have your selection values in the database so you only need to show them to the user now!

2. Creating the view

Now that you have the database part ready it is time to create your view. In this example I will create a simple form view which shows the field ‘name’ and the statusbar with some buttons:

So what does this tell us? Let us break this down part by part. The first thing you will notice is the field header.

Everything within these tags will be shown at the top of your Odoo form.
In this code example it will create four buttons and it will also show the field ‘state’. The field ‘state’ has a widget set to it that is made by Odoo and which will make the typical statusbar, just by adding a widget! This will generate the following bar at the top of your Odoo screen:
Statusbar and button example
Now there is one more thing I should explain, the buttons. By using those buttons we will modify in which state we are on the statusbar (selection) and we will control the whole flow. Let us take the button ‘In progress’ as an example to explain the code:

So what does this do exactly? It gives the button the text ‘In progress’ and we’ll give the button the type object, this will make it accessible in a function from the Python code later on. The name of the button will be the name of the function in your Python code and last but not least is the attrs. The attrs is a handy feature to make buttons visible/invisible when you’re in a state for example. In this example the button ‘In progress’ will not be shown when the statusbar is in the state ‘progress’.
Tip: If you have multiple states where buttons should be hidden you can use domains! For example:[(‘state’,’=’,[‘progress’,’finished’])]
Makes sense right? Let us continue and create the button functions!

3. Adding functions for the buttons

Now that the view is ready and you have buttons they should also trigger something. Open up your Python file and create a function for all of those button names you have. Every button will have a separate Python function and will modify the current record with a write. In this tutorial I will create four functions that simply change the state of what the current record is in:

When you now click on the button ‘In progress’ it will trigger the function ‘progress_progressbar’. The self.write will be triggered, which is telling Odoo that it should write on the current record, and it will modify the state to ‘finished’. Depending on in which state you are you’ll see buttons showing up and dissapearing. This is controller by the attrs value on the buttons. For example when I save my record and set it to in progress:
Statusbars in progress

4. Conclusion

Statusbars (selections) are very handy in Odoo and give you the ability to show the progress to the user in a visual way. Statusbars are ideal to use in combination with buttons which will modify the state where a record is in. Another great thing about statusbars is that they’re always at the exact same location and you can find out what the progress is within seconds.
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

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

Inheriting and modifying QWeb reports

Hi guys,

In this tutorial you will learn how to inherit already existing QWeb reports in a custom module. After this tutorial you will be able to inherit and modify any already existing QWeb report in Odoo!
In this example I will modify the default Quotation / order report through inheritance and I will remove a few elements from the report.

1. Creating a new module

The first step is to create a new module. The correct behaviour in Odoo is to always inherit, don’t ever modify existing files. Start by creating yourself a new module.
Tip: use the scaffold command from Odoo!
Odoo scaffold command
This will create a new module from scratch and the default structure of your module is already there.

2. Creating your XML file

The next step is to open up your XML file (in my example templates.xml) and to create a new record to inherit the report. To inherit a report you have to know in which module it is and what the original XML id of the report is. So, how do you find this?
The easiest way is to go to ‘Settings’ > ‘Reports’ > ‘Reports’ and to search the report you want to modify. In this tutorial this will be the quotation/order report so I’ll search for ‘Order’:
Quotation / Order report
Now that you’ve found your report click on it. This will open a new view where you will find all the technical information you need! For example with the quotation report:
Report details
At the right you will see a clickable link named ‘Search associated QWeb views’. Click on it. This will show you a list of all records that are associated to this specific report. In the example of the quotation report:
QWeb XML views
So this usually shows you two XML records. How do you know which one you need? The one that ends with _document is the correct XML record that you need to inherit. Under the column ‘External ID’ you will see there is an unique name, in this example sale.report_saleorder_document. The first part of this text (sale) is the module where the report is from, the second part (report_saleorder_document) is the name of the report that you want to inherit and modify.

Remember this value and now open up your XML file. To inherit a QWeb report you need two things: an unique template id and the inherit_id. The template id can be chosen by yourself, just make sure its unique. The inherit_id should contain the value you’ve just found on your report (sale.report_saleorder_document).

That is it! You’re now already on the correct report and are inheriting it. So, how do you now add or remove elements? To do this you will need Xpath expressions to find, modify, replace or add elements. Tip: Don’t know how Xpath expressions work? Follow my tutorial here!
For this example I will remove the columns that show the amount, the tax and the price per item. The first step is to modify the table header:

After modifying the table header the table content should also be modified.

This code will remove the fourth, third and second td element and all its content but only for the tbody with class ‘sale_tbody’ and inside the tr.
So this will replace the header and the table content from this report. Have a look at the full code to inherit and modify your QWeb report:

If you would now print out the report you would get this as a result:
Result modified quotation report

3. Adding the dependency for the external QWeb report

The next, and final step, is to add a dependency. Because this QWeb report is inside another module Odoo has to know about this module and its content so you should add a dependency. Without this your module will not work and you will get errors.
Open up your __openerp__.py file in your custom module and find the line with depends.
Now take back that ‘External ID’ from the QWeb report and take the first part of the external id (before the dot). This tells you which module you should add as a dependency:
External ID Qweb report
In this example my QWeb comes from the sale module, so I will add it as a dependency.

4. Conclusion

Thats it! You’re done with inheriting and modifying the QWeb report. When you now install this module in your Odoo you will see the modified report.
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