我们如何在odoo中继承BaseModel类

时间:2017-06-14 10:54:43

标签: abstract-class odoo-9

需要继承BaseModel类并添加一个新属性,如

_auto = True 
_register = False
_name = None
_columns = {}
_constraints = []
_custom = False
_defaults = {}
_rec_name = None
_parent_name = 'parent_id'
_parent_store = False
_parent_order = False
_date_name = 'date'
_order = 'id'
_sequence = None
_description = None
_needaction = False
_translate = True

在所有模型中使用此属性,因此需要声明为全局

示例:

from openerp import api, fields, models, _

class NewClass(models.Model):

    _name = 'object name'
    _new_attribute = 'vaule'

2 个答案:

答案 0 :(得分:1)

from openerp import api, fields, models, _

class BaseModelExtend(models.AbstractModel):
    _name = 'basemodel.extend'
    models.BaseModel._navigation = 'id'

    def _register_hook(self, cr):
        '''
        Register method in BaseModel 
        '''
        @api.multi
        def navigate_field(self):
            #CODE
            return True
        models.BaseModel.navigate_field = navigate_field
        return super(BaseModelExtend, self)._register_hook(cr)

答案 1 :(得分:0)

1-创建一个新的python文件my_basemodel.py

2-添加您的自定义方法

    from openerp.osv.orm import BaseModel
#    built your attribute. you can refer inbuilt code to create attribute.