Using Odoo Wizard to Create Records and Run Methods

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.

from odoo import models, fields, api
class MyCustomWizard(models.TransientModel):
    _name = 'my.custom.wizard'
    _description = 'My Custom Wizard'
    name = fields.Char(string='Name', required=True)
    date_from = fields.Date(string='Date From', required=True)
    date_to = fields.Date(string='Date To', required=True)

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.

<odoo>
    <record id="view_my_custom_wizard_form" 
           model="ir.ui.view">
        <field name="name">
            my.custom.wizard.form
        </field>
        <field name="model">
            my.custom.wizard
        </field>
        <field name="arch" 
               type="xml">
            <form string="My Custom Wizard">
                <group>
                    <field name="name"/>
                    <field name="date_from"/>
                    <field name="date_to"/>
                </group>
                <footer>
                    <button string="Confirm" 
                          type="object" 
                          name="action_confirm" 
                          class="btn-primary"/>
                    <button string="Cancel" 
                          class="btn-secondary" 
                          special="cancel"/>
                </footer>
            </form>
        </field>
    </record>
</odoo>

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

Here is an example of Odoo code using a custom wizard:

from odoo import models, fields, api

class MyCustomWizard(models.TransientModel):
    _name = 'my.custom.wizard'
    _description = 'My Custom Wizard'

    name = fields.Char(string='Name', required=True)
    date_from = fields.Date(string='Date From', required=True)
    date_to = fields.Date(string='Date To', required=True)

    def action_confirm(self):
        # Implement the logic you want the wizard to perform
        # Example: Create a record in another model using the wizard data
        self.env['another.model'].create({
            'name': self.name,
            'date_from': self.date_from,
            'date_to': self.date_to,
        })

        # Optional: Return an action, such as closing the wizard
        return {'type': 'ir.actions.act_window_close'}

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.

<odoo>

    <record id="action_my_custom_wizard" model="ir.actions.act_window">
        <field name="name">My Custom Wizard</field>
        <field name="res_model">my.custom.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
    </record>

    <menuitem id="menu_my_custom_wizard"
        name="My Custom Wizard"
        parent="base.menu_custom"
        action="action_my_custom_wizard" />

</odoo>

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.

Hafiz Junaid 9 سبتمبر, 2024
أرشفة
تسجيل الدخول حتى تترك تعليقاً
Visualise Customer Profitability with QWeb Reports