Python Coding Structure

PEP8 Options

Find Syntax and Semantic Warnings or Errors Using Linter Flake8. The Source Code Must Adhere To Pep8 and Pyflakes Standards With Some of The Exceptions
  • In only
  • F401: Module Imported but Unused


Ordering Status:
  • Standard Library Imports
  • Odoo Imports
  • Known Third Party Imports
  • Local Imports In The Relative Form
  • Imports From Odoo Modules
  • Unknown Third Party Imports
  • The Imported Lines Are Alphabetically Sorted In These 6 Groups.
  • sort Command- Automatically Sort Imports
  • Install With Pip Install Isort And Use With


  • Each Python File Must-Have First Line As-
    # Coding: Utf-8 Or
    # -*- Coding: Utf-8 -*-
  • For Better Translation And Clarity, Use-
    % Over .format()
    %(Varname) Instead Of Positional
  • Favor Readability Over Conciseness Or Use Language Features Or Idioms
  • Use List Comprehension, Dict Comprehension, And Basic Manipulation To Make Code More Pythonic, Easier To Read, And Efficient. Commands Like Map, Filter, Sum, And Others Can Be Used
  • For Recordset Methods, Use Filtered, Mapped, Sorted, And Other Such Commands
  • Some Exceptions:
    From Openerp.exceptions Import Warning As User error (V8)
    From Openerp.exceptions Import User error (V9)
    Find More Appropriate Exceptions In
  • To Document Your Code-
    Docstring On Methods That Explains Purpose Of A Function Instead Of Summary Of Code
    Simple Comments For The Parts Of Code That Are Not Immediately Obvious
    Comments Are A Sign That The Code Is Unreadable And Needs To Be Refactored
  • Variable/class/method Names Must Be Meaningful
  • Refactor Long Functions Into Smaller Ones Used In Loops
  • Use Named Parameter If The Argument To A Function Call Is Not Immediately Obvious
  • Prefer Using English Variable Names And Write Comments In English.
  • Strings That Needs To Be Displayed In Other Languages Must Be Translated Using the Translation System
  • Avoid Using API.v7 Decorator In New Code Unless There Is Already An API Fragmentation In Parent Methods


Odoo Python Classes
  • Use Uppercamelcase For Coding In API V8
    Class Accountinvoice(Models.model):
  • Use Underscore Lowercase Notation For Old API
    Class Account_invoice(Orm.model):
Variable Names
  • For Common Variables, Use Underscore Lowercase Notation
  • For Global Variables Or Constants, Use Underscore Uppercase Notation
  • As New Api Works With Records Or Recordsets Instead Of Id Lists, Do Not Suffix Variable Names With _id Or _ids If There Aren't Any


No SQL Injection
  • When using manual SQL queries, do not introduce SQL injection vulnerabilities
  • If user input is incorrectly filtered or badly quoted or allow an attacker to introduce undesirable clauses to SQL queries, the vulnerabilities are present.
  • The best way to deal with this and stay safe is- never use Python string concatenation (+) or string parameters interpolation (%) to pass variables to SQL query string
  • The database abstraction layer (psycopg2) decides how to format query parametersVariable Names
Never Commit the Transaction
  • The OpenERP/OpenObject framework provides the transactional context for all RPC calls.
  • The principle is- a new database cursor is opened at the beginning of each RPC call and is committed on the returning of call.
  • If error occurs during the execution of RPC call, the transaction is being rolled back atomically while preserving the state of the system
  • The system provides dedicated transaction during execution of test suites so that it can be rolled back or not depending on the server startup option
  • Some consequences of manually call cr.commit()-
    Inconsistent business data and data loss
    Workflow desynchronization, documents stuck permanently
    Test will not have clean roll back and it will start polluting database as well as triggering error


Model Names
  • Use Dot Lowercase Name Of Models. Example- Sale.order
  • Use Name In Singular Form. Example- Sale.order Instead Of Sale.orders
Method Conventions
  • Compute Field: The Compute Method Pattern Is __compute__<field_name>
  • Search Method: The Search Method Pattern Is __search__<field_name>
  • Inverse Method: The Inverse Method Pattern Is __inverse__<field_name>
  • Default Method: The Default Method Pattern Is __default__<field_name>
  • Constraint Method: The Constraint Method Pattern Is __check__<constraint_name>
  • Onchange Method: The Onchange Method Pattern Is __onchange__<field_name>
  • Action Method: An Object Action Method Is Prefix With Action__. The Decorator Of Action Method Is @api.multi, But If It Is Going To Be Used With One Record, Add Self.ensure_one() At The Beginning Of The Method.
  • Method:for V8, You Can Use @api.multi, For Compatibilitty With V9 Use
Model Attribute Order
  • Private Attributes
  • Default Method And __default_get
  • Field Declarations
  • Compute And Search Methods
  • Constraint Methods And Onchange Methods
  • Crud Methods
  • Action Methods
  • Other Business Methods 

Do Not Bypass ORM

  • Never Use Database Cursor Directly When Orm Serves The Same Purpose. If Done, You Will Bypass All Orm Features, Even The Transactions, Access Rights And Others.