r way of adding fields to a model is working, but limits you in what you can do with that field. If you need to do calculations based on that field, you cannot do it without modifying python code.
Also when you have done a lot of modifications like this, and you need to setup another database, you have to do it all over again, manually (this will give errors, because you forget one, or setup one field a bit different than on the other server).
The preferred method is to create a new module, and install that module to get the new field(s) and method(s).
Example of module:
my_module (folder with following files)
- __init__.py
- __openeerp__.py
- my_module_code.py
- my_module_code_view.xml
__init__.py
#name of python file with code
import my_module_code
__openerp__.py
{
'name': 'Name of module,
'version': '0.1',
'category': 'Others',
'complexity': "normal",
'description': """ Some description of your module """
'author': ['Your name'],
'website': 'my.website.com',
'depends': [
"product" #name of module which is needed to be installed
],
'init_xml': [
#place here the XML with initial data
],
'update_xml': [
"my_module_code_view.xml",
],
'demo_xml': [],
'test': [],
'installable': True,
'auto_install': False,
'application': False,
'images': [],
'js': [],
}
my_module_code.py
from openerp.osv import fields, osv
class product_product(osv.osv):
"""
Modified product.product
"""
_inherit = "product.product"
_columns = {
'my_new_field': fields.char('LABEL new Field', select=True,
help='HELP text when hovering over the label in openERP'),
}
def my_method(self, cr, uid, ids):
"""Description of my_method """
#do your actions here
def existing_method(self, cr, uid, ... ):
""" Override existing method, use same parameters as original """
# do some action
res = super(product_product, self_).existing_method(cr, uid, ...)
# do some more actions
return res #might be modified
my_module_code_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Adding the fields from product.product to the screen -->
<record id="product_normal_form_view" model="ir.ui.view">
<field name="name">base.product.defaults.product.form</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<field name='default_code' position='before'>
<field name="my_new_field"/>
</field>
</field>
</record>
</data>
</openerp>