Develop a new module

Develop a new module in Odoo


    Odoo modules are python packages which are specified using addons path option. In this page we'll be creating a new module named 'Customer Feedback'. This will be a simple module just to save customer feedback in Odoo backend

  • Create an empty directory named customer_feedback
    • This will be the technical name of our module 
    • In Odoo apps menu you can search for the module using its name or technical name.
  • Create a file named __init__.py in your customer_feedback directory.
    • Leave it empty for now
  • Create a file named __manifest__.py inside customer_feedback directory
    • An Odoo module is declared by its manifest file
Add these lines to your 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'],
    'application': True,
}


    • Summary is the line shown when you hover over an app in Odoo app store
    • I guess function of all fields in manifest are understood from the name itself.
    • There are many more fields that can be used in manifest file like price, company, website etc.
  • Create a directory named models inside customer_feedback
    • Models are declared as python classes
    • First lets create a python file named customer_feedback_details.py inside directory models
copy these files to your customer_feedback_details.py

Code:
# -*- coding: utf-8 -*-
from odoo import models, fields


class CustomerFeedbackModel(models.Model):
    _name = 'customer.feedback'
    _description = 'Customer Feedback Details'

    customer_id = fields.Many2one('res.partner', string='Customer')
    feedback_type = fields.Many2one('feedback.type', string='Feedback Type')
    rating = fields.Selection([('5', 'Pleased'), ('4', 'Pleased'), ('3', 'Neutral'), ('2', 'Bad'), ('1', 'Worst')],
                              string='Customer Rating', help="Select the rating given by customer.")
    customer_description = fields.Text(string='Description')


class FeedbackTypeModel(models.Model):
    _name = 'feedback.type'
    _description = 'Feedback types'

    name = fields.Char(string='Name')




  • create an __init__.py file in the directory models and import customer_feedback_details

Code:
# -*- coding: utf-8 -*-
from . import customer_feedback_details


 

  • import models inside the __init__.py file in main directory

Code:
# -*- coding: utf-8 -*-
from . import models



    • We have created two new models customer.feedback and feedback.type
    • These models are saved inside the database as customer_feedback and feedback_type table respectively.
    • All fields in the model will be available as columns in the table
    • res.partner is the model to save customer details in Odoo.
  • Create a directory named views
    • In Odoo views are generated using xml.
    • Now we'll be creating form, tree view for the model customer feedback. and a menu and menu action to view both models we created.
Inside views create an xml file named customer_feedback_view.xml

code:
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <record id="customer_feedback_form_view" model="ir.ui.view">
        <field name="name">Customer Feedback Form</field>
        <field name="model">customer.feedback</field>
        <field name="arch" type="xml">
            <form string="Customer Feedback Form">
                <sheet>
                    <group>
                        <group>
                            <field name="customer_id"/>
                            <field name="feedback_type"/>
                        </group>
                        <group>
                            <field name="rating"/>
                        </group></group>
                    <group>
                        <field name="customer_description" placeholder="Description"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id ="customer_feedback_tree_view" model="ir.ui.view">
        <field name="name">Customer Feedback Tree</field>
        <field name="model">customer.feedback</field>
        <field name="arch" type ="xml">
            <tree>
                <field name="customer_id"/>
                <field name="feedback_type"/>
                <field name="rating"/>
            </tree>
        </field>
    </record>

    <record id="action_customer_feedback" model="ir.actions.act_window">
        <field name="name">Customer Feedback</field>
        <field name="res_model">customer.feedback</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="help" type="html">
            <p class="oe_view_non_content_smiling_face">
                Create records of your customer feedback
            </p>
        </field>
    </record>

    <record id="action_feedback_type" model="ir.actions.act_window">
        <field name="name">Feedback Types</field>
        <field name="res_model">feedback.type</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="help" type="html">
            <p class="oe_view_non_content_smiling_face">
                You can add new feedback type here
            </p>
        </field>
    </record>

    <menuitem id="main_customer_feedback_menu" name="Feedback"/>
    <menuitem id="customer_feedback_menu" parent="main_customer_feedback_menu" name="Feedback" sequence="30"/>
    <menuitem id="customer_feedback_details_menu"
              parent="customer_feedback_menu"
              name="Customer Feedback"
              action="action_customer_feedback"
              sequence="10"/>
    <menuitem id="feedback_type_menu"
              parent="customer_feedback_menu"
              name="Feedback Type"
              action="action_feedback_type"
              sequence="20"/>
</odoo>


    • A menu for an action should be defined only after defining the action
  • Final step is to assign which all user_groups have access to models in the this module. Create a new directory named security
create a new file named ir.model.access.csv

code:
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
customer_feedback_user,customer.feedback.user,model_customer_feedback,base.group_user,1,1,1,1
feedback_type_user,feedback.type.user,model_feedback_type,base.group_user,1,1,1,1



    •  Currently I've given read, write, create and delete permission for user group group_user. It is by default applied to all internal users in Odoo
Now we need to define the xml and csv data in our manifest file. So our final manifest file look like


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.model.access.csv',
        'views/customer_feedback_view.xml',
    ],
    'application': True,
}




The structure of our module should look like this now
  • customer_feedback
    • models
      • __init__.py
      • customer_feedback_details.py
    • security
      • ir.model.access.csv
    • views
      • customer_feedback_view.xml
    • __init__.py
    • __manifest__.py 

 Congrats!!! Your module is ready to install now.

Next Chapter: How to install an Odoo module






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...