Chapter 10: Ready For Some Action?
So far we have mostly built our module by declaring fields and views. We just introduced business logic in the previous chapter thanks to computed fields and onchanges. In any real business scenario, we would want to link some business logic to action buttons. In our real estate example, we would like to be able to:
cancel or set a property as sold
accept or refuse an offer
One could argue that we can already do these things by changing the state manually, but this is not really convenient. Moreover, we want to add some extra processing: when an offer is accepted we want to set the selling price and the buyer for the property.
In our real estate module, we want to link business logic with some buttons. The most common way to do this is to:
Add a button in the view, for example in the
headerof the view:
and link this button to business logic:
type="object" to our button, the Odoo framework will execute a Python method with
name="action_do_something" on the given model.
The first important detail to note is that our method name isn’t prefixed with an underscore (
_). This makes our method a public method, which can be called directly from the Odoo interface (through an RPC call). Until now, all methods we created (compute, onchange) were called internally, so we used private methods prefixed by an underscore. You should always define your methods as private unless they need to be called from the user interface.
Also note that we loop on
self. Always assume that a method can be called on multiple records; it’s better for reusability.
Finally, a public method should always return something so that it can be called through XML-RPC. When in doubt, just
In Chapter 6: Finally, Some UI To Play With, we created an action that was linked to a menu. You may be wondering if it is possible to link an action to a button. Good news, it is! One way to do it is:
type="action" and we refer to the external identifier in the
In the next chapter we’ll see how we can prevent encoding incorrect data in Odoo.