2
.. i18n: Appendices A : Coding Conventions
3
.. i18n: =================================
6
Appendices A : Coding Conventions
7
=================================
10
.. i18n: -------------
16
.. i18n: Use tabs: will be replaced by spaces soon...
19
Use tabs: will be replaced by spaces soon...
21
.. i18n: Take care with default values for arguments: they are only evaluated once when the module is loaded and then used at each call. This means that if you use a mutable object as default value, and then modify that object, at the next call you will receive the modified object as default argument value. This applies to dict and list objects which are very often used as default values. If you want to use such objects as default value, you must either ensure that they won't be modified or use another default value (such as None) and test it. For example:
24
Take care with default values for arguments: they are only evaluated once when the module is loaded and then used at each call. This means that if you use a mutable object as default value, and then modify that object, at the next call you will receive the modified object as default argument value. This applies to dict and list objects which are very often used as default values. If you want to use such objects as default value, you must either ensure that they won't be modified or use another default value (such as None) and test it. For example:
26
.. i18n: .. code-block:: python
28
.. i18n: def foo(a=None):
30
.. i18n: if a is None:
37
.. code-block:: python
47
.. i18n: This is what is [in the Python documentation]. In addition it is good practice to avoid modifying objects that you receive as arguments if it is not specified. If you want to do so, prefer to copy the object first. A list can easily be copied with the syntax
50
This is what is [in the Python documentation]. In addition it is good practice to avoid modifying objects that you receive as arguments if it is not specified. If you want to do so, prefer to copy the object first. A list can easily be copied with the syntax
52
.. i18n: copy = original[:]
57
.. i18n: A lot of other objects, such as dict, define a copy method.
60
A lot of other objects, such as dict, define a copy method.
69
.. i18n: The structure of a module should be like this::
76
.. i18n: /module_other.py
77
.. i18n: /module_view.xml
78
.. i18n: /module_wizard.xml
79
.. i18n: /module_report.xml
80
.. i18n: /module_data.xml
81
.. i18n: /module_demo.xml
84
.. i18n: /wizard_name.py
89
.. i18n: /report_name.sxw
90
.. i18n: /report_name.rml
91
.. i18n: /report_name.py
94
The structure of a module should be like this::
118
.. i18n: Naming conventions
119
.. i18n: ------------------
125
.. i18n: * modules: modules must be written in lower case, with underscores. The name of the module is the name of the directory in the addons path of the server. If the module depends on other modules, you can write several module names separated by underscores, starting by the most important name. Example:
127
.. i18n: + sale_commission
129
.. i18n: * objects: the name of an object must be of the form name_of_module.name1.name2.name3.... The namei part of the object must go from the most important name to the least important one, from left to right, in lower case. Try not to use plurals in object names and to avoid shortcuts in the names. Example:
130
.. i18n: + sale.order
131
.. i18n: + sale.order.line
133
.. i18n: + sale_commission.commission.rate
135
.. i18n: * fields: field must be in lowercase, separated by underscores. Try to use commonly used names for fields: name, state, active, partner_id, eso. Conventions for the field name depends on the field type:
136
.. i18n: + many2one: must end by '_id' (eg: partner_id, order_line_id)
137
.. i18n: + many2many: must end by '_ids' (eg: category_ids)
138
.. i18n: + one2many: must end by '_ids' (eg: line_ids
141
* modules: modules must be written in lower case, with underscores. The name of the module is the name of the directory in the addons path of the server. If the module depends on other modules, you can write several module names separated by underscores, starting by the most important name. Example:
145
* objects: the name of an object must be of the form name_of_module.name1.name2.name3.... The namei part of the object must go from the most important name to the least important one, from left to right, in lower case. Try not to use plurals in object names and to avoid shortcuts in the names. Example:
149
+ sale_commission.commission.rate
151
* fields: field must be in lowercase, separated by underscores. Try to use commonly used names for fields: name, state, active, partner_id, eso. Conventions for the field name depends on the field type:
152
+ many2one: must end by '_id' (eg: partner_id, order_line_id)
153
+ many2many: must end by '_ids' (eg: category_ids)
154
+ one2many: must end by '_ids' (eg: line_ids