Odoo

OPENERP 对象字段的定义

OpenERP 对象支持的字段类型有,基础类型:char, text, boolean, integer, float, date, time, datetime, binary;复杂类型:selection, function, related;关系类型:one2one, one2many, many2one, many2many。下面逐一说明。

  • 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')。

  • one2one: 一对一关系,格式为:fields.one2one(关联对象 Name, 字段显示名, ... )。在 V5.0 以后的 版本中不建议使用,而是用 many2one 替代。

  • 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','cate
    gory_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")] 。
  • 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'。

  • property: 属性字段,下面以具体例子解说 property 字段类型。 'propertyproduct_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 中有 一条关联记录。这可以在安装时导入该条记录,参考代码如下: property_product_pricelist