openerp经典收藏 对象的预定义方法(转载)

时间:2021-09-07 10:41:51

对象的预定义方法

原文:http://shine-it.net/index.php/topic,2159.15.html

每个OpenERP的对象都有一些预定义方法,这些方法定义在基类osv.osv中。这些预定义方法有:
基本方法:create, search, read, browse, write, unlink。
    def create(self, cr, uid, vals, context={})
    def search(self, cr, uid, args, offset=0, limit=2000)
    def read(self, cr, uid, ids, fields=None, context={})
    def browse(self, cr, uid, select, offset=0, limit=2000)
    def write(self, cr, uid, ids, vals, context={})
    def unlink(self, cr, uid, ids)

缺省值存取方法:default_get, default_set。
def default_get(self, cr, uid, fields, form=None, reference=None)
def default_set(self, cr, uid, field, value, for_user=False)

特殊字段操作方法:perm_read, perm_write
def perm_read(self, cr, uid, ids)
def perm_write(self, cr, uid, ids, fields)

字段(fields)和视图(views)操作方法:fields_get, distinct_field_get, fields_view_get
def fields_get(self, cr, uid, fields = None, context={})
def fields_view_get(self, cr, uid, view_id=None, view_type='form',context={})
def distinct_field_get(self, cr, uid, field, value, args=[], offset=0,limit=2000)

记录名字存取方法:name_get, name_search
def name_get(self, cr, uid, ids, context={})
def name_search(self, cr, uid, name='', args=[], operator='ilike',context={})

create方法:在数据表中插入一条记录(或曰新建一个对象的resource)。
格式:def create(self, cr, uid, vals, context={})
参数说明:
vals: 待新建记录的字段值,是一个字典,形如: {'name_of_the_field':value, ...}
context (optional): OpenERP几乎所有的方法都带有参数context,context是一个字典,存放一些上下文值,例如当前用户的信息,包括语言、角色等。context可以塞入任何值,在action定义中,有一个context属性,在界面定义时,可以在该属性中放入任何值,context的最初值通常来自该属性值。
返回值:新建记录的id。
举例:id = pooler.get_pool(cr.dbname).get('res.partner.event').create(cr, uid,{'name': 'Email sent through mass mailing','partner_id': partner.id,'description': 'The Description for Partner Event'})

search方法:查询符合条件的记录。
格式:def search(self, cr, uid, args, offset=0, limit=2000)
参数说明:
args: 包含检索条件的tuples列表,格式为: [('name_of_the_field', 'operator', value), ...]。可用的operators有:
  =, >, <, <=, >=
  in
  like, ilike
  child_of
更详细说明,参考《OpenERP应用和开发基础》中的“域条件”有关章节。

· offset (optional): 偏移记录数,表示不返回检索结果的前offset条。
· limit (optional): 返回结果的最大记录数。
返回值:符合条件的记录的id list。

read方法:返回记录的指定字段值列表。
格式:def read(self, cr, uid, ids, fields=None, context={})
参数说明:
· ids: 待读取的记录的id列表,形如[1,3,5,...]
· fields (optionnal): 待读取的字段值,不指定的话,读取所有字段。
· context (optional): 参见create方法。
返回值:返回读取结果的字典列表,形如 [{'name_of_the_field': value, ...}, ...]

browse方法:浏览对象及其关联对象。从数据库中读取指定的记录,并生成对象返回。和read等方法不同,本方法不是返回简单的记录,而是返回对象。返回的对象可以直接使用"."存取对象的字段和方法,形如"object.name_of_the_field",关联字段(many2one等),也可以通过关联字段直接访问“相邻”对象。例如:
    addr_obj = self.pool.get('res.partner.address').browse(cr, uid, contact_id)
    nom = addr_obj.name
    compte = addr_obj.partner_id.bank
这段代码先从对象池中取得对象res.partner.address,调用它的方法browse,取得id=contact_id的对象,然后直接用"."取得"name"字段以及关联对象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