Enable Dark Mode!
symbols-and-conventions-in-odoo.jpg
By: Nimisha Muralidhar

How Symbols and Conventions Are Used Effectively in Odoo

Technical Odoo 14

Symbols are the signs or abbreviations used to indicate specific things in the programming language. 
Conventions are the method that we practice in a specific programming language.
In Odoo we follow certain symbols and conventions, which makes the programming simpler and easy to understand. It is important to follow the symbols and conventions in your custom modules, which will keep your code compatible with that of Odoo modules.
In this blog, we are going to discuss what are the basic symbols and conventions used in Odoo.
Model Name:
1) Use dot notation
2) Module name should be added as prefix (account.move, where module name is account)
Use singular form while defining an Odoo model. For ex: sale.order, res.partner or any other. Never use res.partners or sale.orderS.
Use <relational_base_model>.<action> when defining a transient model (wizard), relational_base_model is the base model which is defined in models i.e. related to transient. “action” is what the transient model does. Avoid word wizard as action.
Eg: account.move.make, project.task.delegate.batch
Use <relational_base_model>.report.<action> based on the transient convention when defining a report model.
Odoo Python Class:
Use camel case for the python classes in Odoo (Object oriented)
class AccountInvoice(models.Model):
Variable Name:
1. Use camel case model variables.
2. For common variables use underscore and lowercase notations.
3. If a variable contains record ID or list of ID use suffix with ID or IDs respectively.
4. one2many and many2many should have IDs at the suffix (Eg - invoice_line_ids,sale_order_line_ids)
5. many2one should contain ID as the suffix (Eg- user_id, partner_id)
An example for the variable name is given below,
Partner = self.env['res.partner']
partners = Partner.browse(ids)
partner_id = partners[0].id
Method Conventions:
Here we discuss the name conventions for the methods in Odoo.
* Compute method - Use compute method name as _compute_<field_name>
* Search method - Use search method name as _search_<field_name>
* Default method - Use default method name as _default_<field_name>
* Selection method - Use selection method name as _selection_<field_name>
* Onchange method - Use onchange method name as _onchange_<field_name>
* Constraint method - Use constraint method name as _check_<field_name>
* Action method - Prefix of action method is _action. It uses on one record so make sure to add self.ensure_one() at the beginning of the method
Order of model attribute:
1. Private attributes - name, order inherited model, etc (_name,_order,_inherit)
2. Default methods and _default_get
3. Field declarations
4. Compute, inverse, search methods in the same order of the field declarations.
5. Selection methods - Methods that return computed values for the selection fields.
6. Constraints (@api.constrains) and onchange methods (@api.onchange)
7. CRUD methods - ORM overrides
8. Action methods
9. Other business methods
An example that follows the order of methods is given below:
class Event(models.Model):
    # Private attributes
    _name = 'event.event'
    _description = 'Event'
    # Default methods
    def _default_name(self):
        ...
    # Fields declaration
    name = fields.Char(string='Name', default=_default_name)
    seats_reserved = fields.Integer(string='Reserved Seats', store=True
        readonly=True, compute='_compute_seats')
    seats_available = fields.Integer(string='Available Seats', store=True
        readonly=True, compute='_compute_seats')
    price = fields.Integer(string='Price')
    event_type = fields.Selection(string="Type", selection='_selection_type')
    # compute and search fields, in the same order of fields declaration
    @api.depends('seats_max', 'registration_ids.state', 'registration_ids.nb_register')
    def _compute_seats(self):
        ...
    @api.model
    def _selection_type(self):
        return []
    # Constraints and onchanges
    @api.constrains('seats_max', 'seats_available')
    def _check_seats_limit(self):
        ...
    @api.onchange('date_begin')
    def _onchange_date_begin(self):
        ...
    # CRUD methods (and name_get, name_search, ...) overrides
    def create(self, values):
        ...
    # Action methods
    def action_validate(self):
        self.ensure_one()
        ...
    # Business methods
    def mail_user_confirm(self):
        ...
These are the basic symbols and conventions in Odoo. 
Happy Coding!


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

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

location

Kochi

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

location

Bangalore

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

Send Us A Message