模块开发实例 (Story 1)继承模型

来源:互联网 发布:金山 雷军 知乎 编辑:程序博客网 时间:2024/05/18 21:47

继承

在OpenERP里面继承机制很好的实现对于原有模块的修改,多用于添加,修改字段和方法。

用户故事

作为小超市店长,用户希望能对商品编号不能重复。

包结构

OpenERP模块结构

__init__.py

__terp__.py

product.py

__init__.py

import product

就像其他的Python模块,是程序的初始化文件,在里面会写了那些需要导入的文件

在本例子的是导入product.py,so..

 

__terp__.py

{    'name' : 'product_code_extend',    'version' : "1.0",    'depends' : ['product'],    'init_xml' : [],    'update_xml' : [],    'installable' : True,    'active' : False,    'author' : 'joshua',    'website': 'http://openerp-china.org',}

terp是OpenERP模块的说明文件,也是用python语言编写的,这个文件最重要的用途是

1.确定有哪些XML文件是需要在初始化的时候导入到server里面的。(此例子没有)

2.确定在创建这个模块的时候有哪些依赖的模块。(这里的product模块)

product.py

 

from osv import fieldsfrom osv import osvclass product_product(osv.osv):    _name = 'product.product'    _inherit = 'product.product'    _sql_constraints = [       ('default_code_product_uniq', 'unique(default_code,id)', 'The code of the product must be unique!')    ]product_product()

关于里面属性的详尽含义可以看这里

_inherit = ‘product.product’这个是你要继承的对象,在addonproductproduct.py里面可以查看到本来的product模块,我们这里是想按照code来排序,code在原来的product.produt里面是

'default_code' : fields.char('Code', size=64),

所以这里_order = _order = ‘default_code’ 就是讲产品的列表按照编号来排序,而_sql_constraints 则是在数据表里面添加一条这样的约束条件

CONSTRAINT product_product_default_code_product_uniq UNIQUE (default_code)

更新到OpenERP

1.将上面建好的模块放在OpenERP Serveraddons 目录下

Image:product_code_extend_location.jpg

2.Administation->Modules Management->Update Modules List可以更新模块列表。

Image:product_code_extend_update_module_list.jpg

3.在模块列表里面选择product_code_extend,点击Schedule for installation再点击 Apply Scheduled Upgredes更新模块,

Image:product_code_extend_update.jpg

4.重启服务,重新登录,成功。

如果新建的product违反了约束条件,保存的时候就会报错

Image:product_code_error.jpg

调试方法

LOG
self.logger.notifyChannel(‘addons.’+self._name, netsvc.LOG_INFO, ‘value:%s’%res)
XMLRPC
OpenERPServeraddonsquality_integration_serverbase_quality_interrogation.py
Assert

want more,请看调试方法