Odoo Wizards
by Hafiz Junaid
Odoo wizards are an excellent way to create temporary forms that guide users through specific processes, like mass updates or guided data entry. If you're looking to automate repetitive tasks or handle user-driven data processing in a structured way, wizards are the perfect solution. In this blog post, we'll explore how to use Odoo's default wizard functionality within a custom module to create a record and execute a method using Python.
Implementing Odoo Wizard Functionality
Step 1: Create a Wizard Model
The first step in creating a wizard is to define a new model that inherits from TransientModel. Unlike regular models, TransientModel is used to store temporary data that won't be permanently stored in the database. This model will hold the temporary data required by the wizard.
In the example above, we've created a simple wizard model called MyCustomWizard with three fields: name, date_from, and date_to. These fields will capture user input when the wizard is executed.
Step 2: Define a Form View for the Wizard
Next, we need to create a form view that will be presented to the user when they run the wizard. This form will collect the data specified in the wizard model.
In this XML snippet, we're defining a form view for our wizard. The form includes fields for name, date_from, and date_to, as well as buttons for confirming or cancelling the wizard.
The Confirm button is linked to the action_confirm method, which we'll define next.
Step 3: Implement the Method to Run the Wizard's Logic
Now that we have a form view, we need to implement the method that will be executed when the user clicks the Confirm button. This method will contain the business logic that the wizard is meant to perform.
In the action_confirm method, we're using the values entered by the user in the wizard to create a new record in another model (another.model). After the logic is executed, the wizard is closed by returning an action that triggers the window to close.
Note: When creating any record, we first need to check which fields are mandatory in the model. It's a good practice to handle potential validation errors, such as missing mandatory fields, to avoid issues during record creation.
Step 4: Add a Menu Item or Action to Trigger the Wizard
To allow users to access the wizard, you'll need to add a menu item or action that opens the wizard form.
Here, we've defined an action action_my_custom_wizard that opens the wizard form. We've also added a menu item that users can click to trigger the wizard.
Step 5: Test Your Wizard
Once you've completed the steps above, you can test your wizard by navigating to the menu item and filling in the form. While testing, it's useful to check the Odoo logs to ensure the wizard is working correctly and troubleshoot any issues. After confirming, your wizard should perform the desired action (e.g., creating a record in another model).
Tip: This type of wizard can also be extended to perform tasks such as generating reports with a date range in PDF or Excel formats, depending on the data requirements.
Conclusion
Odoo's wizard functionality is a powerful tool for guiding users through specific processes in your custom modules. By implementing wizards, you can save users time and ensure accuracy by structuring complex workflows step by step. By following the steps in this blog post, you can create a wizard in Odoo, capture user input, and execute custom logic with ease.
Need more development tips?
Stay tuned to our blog.