standards-to-follow-while-creating-module-odoo.png
Blogger_637027954641495186.png
By: Ajmal JK

Standards to Follow While Creating a Module in Odoo

Technical

First of all, this blog is not about Coding standards. It is about the standards and traditions one must follow when creating a module in odoo. There are a number of options that one must follow and keep in mind when creating a complete module. 


1. Module Structure and Naming

One has to consider several standards in module naming, model naming, file naming, function naming, etc. You can know more about this from the Coding Standards in Odoo blog.


2. When defining a model

> We have to be specific when defining model names.

Example: For Sale Order, the model name is ‘sale.order’ not ‘sales.order’ or ‘sale.orders’


> When defining a model it is very important to specify the _rec_name.

Example: _rec_name = ‘display_name’.


If we don’t specify the _rec_name, and the model doesn’t contain a field ‘name’, then where ever we refer to this model it shows the combination of model name and id, like below.

(As an example taking a model ‘food.process’)


standards-to-follow-while-creating-module-odoo


And also if we refer this model in any Many2one fields, it will display like below.


standards-to-follow-while-creating-module-odoo


After giving the _rec_name,


standards-to-follow-while-creating-module-odoo


From the above examples, we can understand the importance of _rec_name.


> Like _rec_name, also we have to specify the _description for models.


Example: _description = ‘Food Processing’


> In a model, there should be at least a required field. If we don’t give a required field, then there should be issues like,


standards-to-follow-while-creating-module-odoo


standards-to-follow-while-creating-module-odoo


A number of empty records will be created if we don’t give a required field.


> If the model contains the state field, it must include the tracking parameter in the field definition. (track_visibility=’onchange’)


Example:


state = fields.Selection([
                            ('draft', 'Draft'), ('process', 'Processed'),
                           ('reject', 'Rejected'), ('finish', 'Finished')],
                          string='Status', default='draft',  track_visibility='onchange')


So we can track the state changes done in any model from the chatter window.


standards-to-follow-while-creating-module-odoo


> When creating a Many2one field, define the ondelete parameter if necessary. It will restrict the deletion of records if it is referenced anywhere.


Example:


food_process_id = fields.Many2one('food.process', string="Food Section", ondelete="restrict")


If we set the ondelete parameter as ‘restrict’, then it will restrict the deletion of refered records.


standards-to-follow-while-creating-module-odoo


> When creating a Many2one field, define the ondelete parameter if necessary. Another important thing is,  the ‘read-only’ attribute should set to the fields according to the state. For example, a Sale Order shouldn’t be edited after confirmation.

In this case, a food process order can not be edited after Finished, ie when it is in ‘finish’


Example:


<field name="description" attrs="{'readonly':[('state', '=', 'finish')]}"/>


> Have to restrict the deletion of records according to state. For example, it not possible to delete an Invoice after posted, or a Sale Order which is confirmed. So we have to restrict it according to the record state.


In this case, a food process record can not be deleted if it not in the ‘draft’ state, for this we have to override the unlink method of the model.


Example:


def unlink(self):
for rec in self:
if rec.state != 'draft':
raise UserError(_('You Can not delete a process not in draft state !!'))
return super(FoodProcess, self).unlink()


standards-to-follow-while-creating-module-odoo


Filters and Search View.


> When we create a model we have to define proper search views and filters to increase the use of access. For example, if the model has a state field then it should be possible to ‘group by’ the whole record by state field, and also can filter by some important states.


> For example in Sale order, we can filter out the quotations and sale orders which are based on the state. And we can group by all orders by Customer and Salesperson. Like this, if we define a model we have to provide the appropriate filters, group by and search fields.


standards-to-follow-while-creating-module-odoo



cybrosys youtube

Comments

0


Leave a comment

 
Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message
 
 
 
 
Close
cybrosys