Odoo Build - Develop a new module
In this guide I'll walk you through the development of a module. From specification to the final commit.
Specification
Let's assume have received the following specification.
Develop CRM Lead Template
Module: crm_lead_template
Version: 19.0
Repo: addons/sale_workflow
Depends: crm
Summary: Create lead notes from template.
Add model crm.lead.template. Add menu to configuration.
Model has field description and tag_ids.
Add field crm.lead:template_id -> cr.lead.template. When field is selected, the content of description and tag_ids is copied. Copy field content if is empty.
There are some meta information (Module, Version, Repo, Depends, Summary) and broad description what the module should do.
As an experienced Odoo developer you realize that this functionality also exists for sale order and that you can lookup the implementation there.
Enviroment
It is assumed that the Odoo Build enviroment is running.
We have access to the Odoo source code.
On the terminal we have use zsh git aliases and the helix editor.
Bootstrap
First checkout the correct Odoo version.
[main][~/Odoo-Build]$ task checkout 19.0Then create the module.
[main][~/Odoo-Build]$ task create-module addons/sale_workflow/crm_lead_templateThis command will prompt you for values.
Prompt for module manifest values...
Summary: Create lead notes from template.
License (AGPL-3/OPL-1): AGPL-3
Depends (comma-separated): crm
Demo? (y/n): nLet's add a new model.
[main][~/Odoo-Build]$ task generate-module-model addons/sale_workflow/crm_lead_template crm.lea
d.templateAnd views for this model.
[main][~/Odoo-Build]$ task generate-module-views addons/sale_workflow/crm_lead_template crm.lead.templateWe will extend the crm.lead model as well.
task generate-module-inherit addons/sale_workflow/crm_lead_template crm.leadWe need to update the crm.crm_lead_view_form form.
[main][~/Odoo-Build]$ task generate-module-snippet addons/sale_workflow/crm_lead_template crm.crm_lead_view_form crm.leadDon't forget about the security rules.
[main][~/Odoo-Build]$ task generate-module-security addons/sale_workflow/crm_lead_template crm.
lead.templateMetadata
Let's edit the metadata of the module
[main][~/Odoo-Build]$ hx addons/sale_workflow/crm_lead_template/__manifest__.pyI performed the following actions:
- Renamed name from
CrmtoCRM - Added
"views/crm_lead_views.xml",and"views/crm_lead_template_views.xml",to the data list
Once we updated the metadata we can generate the module docs.
[main][~/Odoo-Build]$ task generate-module-docs addons/sale_workflow/crm_lead_templateModel
According to the spec we need to add two fields the model. Let's lookup the definition.
[main][~/Odoo-Build]$ rg --no-line-number "description = fields" odoo/addons/crm/**/*.py
odoo/addons/crm/models/crm_lead.py
description = fields.Html('Notes')[main][~/Odoo-Build]$ rg --no-line-number "tag_ids = fields" odoo/addons/crm/**/*.py -A2
odoo/addons/crm/report/crm_activity_report.py
tag_ids = fields.Many2many(related="lead_id.tag_ids", readonly=True)
won_status = fields.Selection([
('won', 'Won'),
odoo/addons/crm/models/crm_lead.py
tag_ids = fields.Many2many(
'crm.tag', 'crm_tag_rel', 'lead_id', 'tag_id', string='Tags',
help="Classify and analyze your lead/opportunity categories like: Training, Service")Now we can copy these definition to the model.
[19.0][~/Odoo-Build/addons/sale_workflow/crm_lead_template]$ hx models/crm_lead_template.py- Rename class from
CrmLeadTemplatetoCRMLeadTemplate - Rename name from
CrmtoCRM - Remove
valuefield - Add
descriptionandtag_ids
description = fields.Html("Notes")
tag_ids = fields.Many2many(
"crm.tag",
"crm_template_tag_rel",
"lead_template_id",
"tag_id",
string="Tags",
help="Classify and analyze your lead/opportunity categories like: Training, Service",
)Do not worry about formatting.
[19.0][~/Odoo-Build/addons/sale_workflow/crm_lead_template]$ hx views/crm_lead_template_views.xml- Remove content inside of
header - Add
tags_idsbeforenotebook.
<group>
<field name="tag_ids" widget="many2many_tags" />
</group>- Replace
valuewithdescription.
<group>
<field name="description" placeholder="Add a description..." />
</group>- Remove
valuefrom list and search view. - Replace menu item definition.
<menuitem
id="crm_lead_template.menu_crm_lead_template"
name="Lead Templates"
parent="crm.crm_menu_config"
action="crm_lead_template.action_crm_lead_template_view"
sequence="7"
/>Inherit
Let's have a look at the the sale_management module.
[main][~/Odoo-Build]$ rg --no-line-number "sale_order_template_id = fields" odoo/addons/sale_management/**/*.py -A5
odoo/addons/sale_management/models/sale_order.py
sale_order_template_id = fields.Many2one(
comodel_name='sale.order.template',
string="Quotation Template",
compute='_compute_sale_order_template_id',
store=True, readonly=False, check_company=True, precompute=True,
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]")Let's add the template_id field and the business logic.
[19.0][~/Odoo-Build/addons/sale_workflow/crm_lead_template]$ hx models/crm_lead.py- Rename class from
CrmLeadtoCRMLead - Add the
temlate_idfield
template_id = fields.Many2one(
comodel_name="crm.lead.template",
string="Lead Template",
inverse="_inverse_template_id",
store=True,
readonly=False,
)- Add the
_compute_template_idmethod
def _inverse_template_id(self):
"""
Update templated fiels if they are empty and template is updated.
"""
for lead in self:
if not lead.description:
lead.description = lead.template_id.description
if not lead.tag_ids:
lead.tag_ids = lead.template_id.tag_idsThen update the inherited form.
[19.0][~/Odoo-Build/addons/sale_workflow/crm_lead_template]$ hx views/crm_lead_views.xml- Add
template_idafteruser_id.
<field name="user_id" position="after">
<field name="template_id" />
</field>Lint
In the repo folder update the docs and lint the modules.
[19.0][~/Odoo-Build/addons/sale_workflow]$ task docs
[19.0][~/Odoo-Build/addons/sale_workflow]$ task lintTest
Reset the database.
[main][~/Odoo-Build]$ task drop-db,init-dbAnd install the module.
[main][~/Odoo-Build]$ task init-module addons/sale_workflow/crm_lead_templateIf everything went well, start the Odoo server.
[main][~/Odoo-Build]$ task start sourceCreate a template and and add it on a lead.
Commit
Add changes and push them.
[19.0][~/Odoo-Build/addons/sale_workflow]$ gaa
[19.0][~/Odoo-Build/addons/sale_workflow]$ gcmsg "feat(crm_lead_template): init module"
[19.0][~/Odoo-Build/addons/sale_workflow]$ gp