Creating a Wizard

Wizards can be used for creating interactive sessions with the user. For creating a wizard we need to define a model and form view for the wizard. For a wizard Transientmodel is used instead of Model for the following reasons:
  • Saved records will be automatically deleted after some time.
  • All users have access to the model.
  • Wizard records can access regular model records through Many2one fields, but regular models cannot access wizard records.
Now lets create wizard for our module customer_feedback created in the previous chapters of this tutorial. We'll use this wizard to create a PDF report in the next chapter.

  • Create a new directory named wizard in our main directory customer_feedback
  • create a file named customer_feedback_report_wizard.py. we'll create a model for our wizard in this file.
Code:
# -*- coding: utf-8 -*-
from odoo import api, fields, models


class CustomerFeedbackReportWizard(models.TransientModel):
    _name = 'customer.feedback.report.wizard'
    _description = 'Customer Feedback Report Wizard'

    customer_id = fields.Many2one('res.partner', string='Customer')

    @api.multi
    def print_report(self):
        print('Print function called...')


  • Create __init__.py file inside wizard directory and import python files.
Code:
# -*- coding: utf-8 -*-
from . import customer_feedback_report_wizard


  •  import wizard inside our main directory init file
  • Updated __init__.py should look like
code:
# -*- coding: utf-8 -*-
from . import models
from . import wizard


Creating an act window for calling the wizard.
  • Inside the wizard directory create a new file named customer_feedback_wizard.xml
code:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="customer_feedback_wizard_form_view" model="ir.ui.view">
        <field name="name">Customer Feedback Report Wizard</field>
        <field name="model">customer.feedback.report.wizard</field>
        <field name="arch" type="xml">
        <form string="Customer Feedback Report">
            <group>
                <group>
                    <field name="customer_id" />
                </group>
            </group>
            <footer>
                <button name="print_report" string="Print" type="object" class="btn-primary"/>
                <button string="Cancel" special="cancel" class="btn-secondary"/>
            </footer>
        </form>
        </field>
    </record>

    <act_window id="action_customer_feedback_report"
                name="Customer Feedback Report"
                src_model="customer.feedback"
                res_model="customer.feedback.report.wizard"
                view_mode="form"
                target="new"
                key2="client_action_multi"/>

    <menuitem id="main_report_menu" parent="customer_feedback.main_customer_feedback_menu" name="Reports" sequence="60"/>
    <menuitem
            id="menu_customer_feedback_report"
            name="Customer Feedback Report"
            action="action_customer_feedback_report"
            parent="main_report_menu"/>

</odoo>


  • Define the xml file inside __manifest__.py
  • Updated manifest file
Code:
 # -*- coding: utf-8 -*-
{
    'name': "Customer Feedback",
    'version': '1.0',
    'summary': """Record Your Customer feedback""",
    'description': """
        Customer Feedback Module
            -create feedback to customers
    """,
    'author': "My Company",
    'category': 'Test',

    # any module necessary for this one to work correctly
    'depends': ['base'],
    'data': [
        'security/ir.models.access.csv',
        'views/customer_feedback_view.xml',
        'wizard/customer_feedback_wizard.xml',
        'report/customer_feedback_report.xml',
        'report/customer_feedback_report.xml',
    ],
    'application': True,
}


 Now restart your Odoo instance and upgrade the module customer_feedback to view the changes we've made.

Next Chapter: Creating a report from wizard


No comments:

Post a Comment

How to create XLS report in Odoo 12

In this tutorial we'll be creating a simple XLS report that can be generated from wizard in Sales/ Report menu . XLS reports are made u...