定义模型的时候,和python的其他框架类似,可以对比Django,同样是一个模型即:一个class对应生成数据库中的一张表,

只是odoo的继承机制比较复杂一点,在日常的开发中,定义模型的时候,

基本字段类型有:

Char、Text、Selection、Html、Integer、Float、Date、Datetime、Boolean、Binary。

复杂类型:selection, function, related

用于设置类似主外键关联关系的字段有:

Many2many

Many2one

One2many

下面逐一说明:

基本字段类型

boolean: 布尔型(true, false)
integer: 整数。
float: 浮点型,如 'rate' : fields.float('Relative Change rate',digits=(12,6)), digits定义整数部分和小数部分的位数。
char: 字符型,size属性定义字符串长度。
text: 文本型,没有长度限制。
date: 日期型
datetime: 日期时间型
binary: 二进制型

复杂类型

function: 函数型,该类型的字段,字段值由函数计算而得,不存储在数据表中。其定义格式为:
fields.function(fnct, arg=None, fnct_inv=None, fnct_inv_arg=None, type='float', fnct_search=None, obj=None, method=False, store=True)
· type 是函数返回值的类型。
· method 为True表示本字段的函数是对象的一个方法,为False表示是全局函数,不是对象的方法。如果method=True,obj指定method的对象。
· fcnt 是函数或方法,用于计算字段值。如果method = true, 表示fcnt是对象的方法,其格式如下:def fnct(self, cr, uid, ids, field_name, args, context),否则,其格式如下:def fnct(cr, table, ids, field_name, args, context)。ids是系统传进来的当前存取的record id。field_name是本字段名,当一个函数用于多个函数字段类型时,本参数可区分字段。args是'arg=None'传进来的参数。
· fcnt_inv 是用于写本字段的函数或方法。如果method = true, 其格式是:def fcnt_inv(self, cr, uid, ids, field_name, field_value, args, context),否则格式为:def fcnt_inv(cr, table, ids, field_name, field_value, args, context)
· fcnt_search 定义该字段的搜索行为。如果method = true, 其格式为:def fcnt_search(self, cr, uid, obj, field_name, args),否则格式为:def fcnt_search(cr, uid, obj, field_name, args)
· store 表示是否希望在数据库中存储本字段值,缺省值为False。不过store还有一个增强形式,格式为 store={'object_name':(function_name,['field_name1','field_name2'],priority)} ,其含义是,如果对象'object_name'的字段['field_name1','field_name2']发生任何改变,系统将调用函数function_name,函数的返回结果将作为参数(arg)传送给本字段的主函数,即fnct。
selection: 下拉框字段。定义一个下拉框,允许用户选择值。如:'state': fields.selection((('n','Unconfirmed'),('c','Confirmed')),'State', required=True),这表示state字段有两个选项('n','Unconfirmed')和('c','Confirmed')。

related: 关联字段,表示本字段引用关联表中的某字段。格式为:fields.related(关系字段,引用字段,type, relation, string, ...),关系字段是本对象的某字段(通常是one2many or many2many),引用字段是通过关系字段关联的数据表的字段,type是引用字段的类型,如果type是many2one or many2many, relation指明关联表。例子如下:
        'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts'),
        'city':fields.related('address','city',type='char', string='City'),
        'country':fields.related('address','country_id',type='many2one', relation='res.country', string='Country'),

这里,city引用address的city字段,country引用address的country对象。在address的关联对象res.partner.address中,country_id是many2one类型的字段,所以type='many2one', relation='res.country'。

关联关系的字段

one2one: 一对一关系,格式为:fields.one2one(关联对象Name, 字段显示名, ... )。在V5.0以后的版本中不建议使用,而是用many2one替代。odoo9.0之后不支持一对一该字段类型。
many2one: 多对一关系,格式为:fields.many2one(关联对象Name, 字段显示名, ... )。可选参数有:ondelete,可选值为"cascade"和"null",缺省值为"null",表示one端的record被删除后,many端的record是否级联删除。
one2many: 一对多关系,格式为:fields.one2many(关联对象Name, 关联字段, 字段显示名, ... ),例:'address': fields.one2many('res.partner.address', 'partner_id', 'Contacts')。
many2many: 多对多关系。例如:
   'category_id':fields.many2many('res.partner.category','res_partner_category_rel','partner_id','category_id','Categories'),
表示以多对多关系关联到对象res.partner.category,关联表为'res_partner_category_rel',关联字段为'partner_id''category_id'。当定义上述字段时,OpenERP会自动创建关联表为'res_partner_category_rel',它含有关联字段'partner_id''category_id'

reference: 引用型,格式为:fields.reference(字段名, selection, size, ... )。其中selection是: 1)返回tuple列表的函数,或者 2)表征该字段引用哪个对象(or model)的tuples列表。reference字段在数据库表中的存储形式是(对象名,ID),如(product.product,3)表示引用对象product.product(数据表product_product)中id=3的数据。reference的例子:
    def _links_get(self, cr, uid):
        cr.execute('select object,name from res_request_link order by priority')
        return cr.fetchall()

    ...
    'ref':fields.reference('Document Ref 2', selection=_links_get, size=128),
    ...
上例表示,字段ref可以引用哪些对象类型的resource,可引用的对象类型从下拉框选择。下拉框的选项由函数_links_get返回,是(object,name)对的列表,如[("product.product","Product"), ("account.invoice","Invoice"), ("stock.production.lot","Production Lot")] 。

property: 属性字段,下面以具体例子解说property字段类型。
'property_product_pricelist': fields.property('product.pricelist', type='many2one', relation='product.pricelist',string="Sale Pricelist", method=True, view_load=True, group_name="Pricelists Properties")
这个例子表示,本对象通过字段'property_product_pricelist'多对一(type='many2one')关联到对象product.pricelist(relation='product.pricelist')。和many2one字段类型不同的是,many2one字段会在本对象中创建数据表字段'property_product_pricelist',property字段类型不会创建数据表字段'property_product_pricelist'。property字段类型会从数据表ir.property中查找name='property_product_pricelist'(即字段定义中的'product.pricelist'加上前缀property,并将"."替换成"_"作为name)且company_id和本对象相同的记录,从该记录的value字段(value字段类型为reference)查得关联记录,如(product.pricelist,1),表示本对象的resource多对一关联到对象product.pricelist的id=1的记录。也就是说,property字段类型通过ir.property间接多对一关联到别的对象。
    property字段类型基本上和many2one字段类型相同,但是有两种情况优于many2one字段。其一是,例如,当有多条记录通过ir.property的name='property_product_pricelist'的记录关联到记录(product.pricelist,1),此时,如果希望将所有关联关系都改成关联到记录(product.pricelist,2)。如果是many2one类型,不写代码,很难完成此任务,是property字段的话,只要将ir.property中的value值(product.pricelist,1)改成(product.pricelist,2),则所有关联关系都变了。修改ir.property的value值可以在系统管理下的菜单Configuration --> Properties中修改。其二是,例如,同一业务伙伴,但希望A公司的用户进来看到的该业务伙伴价格表为pricelistA,B公司的用户进来看到的该业务伙伴价格表为pricelistB,则many2one类型达不到该效果。property类型通过ir.property中的记录关联时加上了company_id的条件,因此可以使得不同公司的员工进来看到不同的关联记录。
    由于property类型通过ir.property关联,因此,每个property类型的字段都必须在ir.property中有一条关联记录。这可以在安装时导入该条记录,参考代码如下:
    <record model="ir.property" 字段以及关联对象patner的银行(addr_obj.partner_id.bank)。

格式:def browse(self, cr, uid, select, offset=0, limit=2000)
参数说明:
select: 待返回的对象id,可以是一个id,也可以是一个id 列表。
· offset (optional): 参见search方法。
· limit (optional): 参见search方法。
返回值:返回对象或对象列表。
注意:本方法只能在Server上使用,由于效率等原因,不支持rpc等远程调用。

write方法:保存一个或几个记录的一个或几个字段。
格式:def write(self, cr, uid, ids, vals, context={})
参数说明:
· ids: 待修改的记录的id列表。
· vals: 待保存的字段新值,是一个字典,形如: {'name_of_the_field': value, ...}。
· context (optional): 参见create方法。
返回值:如果没有异常,返回True,否则抛出异常。
举例:self.pool.get('sale.order').write(cr, uid, ids, {'state':'cancel'})

unlink方法:删除一个或几个记录。
格式:def unlink(self, cr, uid, ids)
参数说明:
· ids: 待删除的记录的id列表。
返回值:如果没有异常,返回True,否则抛出异常。


default_get方法:复位一个或多个字段的缺省值。
格式: def default_get(self, cr, uid, fields, form=None, reference=None)
参数说明:
• fields: 希望复位缺省值的字段列表。
• form (optional): 目前似乎未用(5.06版)。
• reference (optional): 目前似乎未用(5.06版)。
返回值: 字段缺省值,是一个字典,形如: {'field_name': value, ... }。
举例:self.pool.get('hr.analytic.timesheet').default_get(cr, uid, ['product_id','product_uom_id'])

default_set方法:重置字段的缺省值。
格式: def default_set(self, cr, uid, field, value, for_user=False)
参数说明:
• field: 待修改缺省值的字段。
• value: 新的缺省值。
• for_user (optional): 修改是否只对当前用户有效,还是对所有用户有效,缺省值是对所有用户有效。
返回值: True

相关文章: