每个OpenERP的对象都有一些预定义方法,这些方法定义在基类osv.osv中。
基本方法:create, search, read, browse, write, unlink。
def create(self, cr, uid, vals, 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'})
def search(self, cr, uid, args, offset=0, limit=2000)
#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。
def read(self, cr, uid, ids, fields=None, context={})
#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, ...}, ...]
def browse(self, cr, uid, select, offset=0, limit=2000)
#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等远程调用
def write(self, cr, uid, ids, vals, context={})
#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'})
def unlink(self, cr, uid, ids)
#unlink方法:删除一个或几个记录。
格式:def unlink(self, cr, uid, ids)
#参数说明:
· ids: 待删除的记录的id列表。
返回值:如果没有异常,返回True,否则抛出异常。
缺省值存取方法:default_get, default_set。
def default_get(self, cr, uid, fields, form=None, reference=None)
#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'])
def default_set(self, cr, uid, field, value, for_user=False)
#default_set方法:重置字段的缺省值。
格式: def default_set(self, cr, uid, field, value, for_user=False)
#参数说明:
• field: 待修改缺省值的字段。
• value: 新的缺省值。
• for_user (optional): 修改是否只对当前用户有效,还是对所有用户有效,缺省值是对所有用户有效。
返回值: True
特殊字段操作方法:perm_read, perm_write
def perm_read(self, cr, uid, ids)
def perm_read(self, cr, user, ids, context=None, details=True):
"""
Returns some metadata about the given records.
:param details: if True, \*_uid fields are replaced with the name of the user
:return: list of ownership dictionaries for each requested record
:rtype: list of dictionaries with the following keys:
* id: object id
* create_uid: user who created the record
* create_date: date when the record was created
* write_uid: last user who changed the record
* write_date: date of the last change to the record
* xmlid: XML ID to use to refer to this record (if there is one), in format ``module.name``
"""
......................
def perm_write(self, cr, uid, ids, fields)
def perm_write(self, cr, user, ids, fields, context=None):
raise NotImplementedError(_('This method does not exist anymore'))
字段(fields)和视图(views)操作方法:fields_get, distinct_field_get, fields_view_get
def fields_get(self, cr, uid, fields = None, context={})
def fields_get(self, cr, user, allfields=None, context=None, write_access=True):
""" Return the definition of each field.
The returned value is a dictionary (indiced by field name) of
dictionaries. The _inherits'd fields are included. The string, help,
and selection (if present) attributes are translated.
:param cr: database cursor
:param user: current user id
:param allfields: list of fields
:param context: context arguments, like lang, time zone
:return: dictionary of field dictionaries, each one describing a field of the business object
:raise AccessError: * if user has no create/write rights on the requested object
"""
def fields_view_get(self, cr, uid, view_id=None, view_type='form',context={})
def fields_view_get(self, cr, user, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
"""
Get the detailed composition of the requested view like fields, model, view architecture
:param cr: database cursor
:param user: current user id
:param view_id: id of the view or None
:param view_type: type of the view to return if view_id is None ('form', tree', ...)
:param context: context arguments, like lang, time zone
:param toolbar: true to include contextual actions
:param submenu: deprecated
:return: dictionary describing the composition of the requested view (including inherited views and extensions)
:raise AttributeError:
* if the inherited view has unknown position to work with other than 'before', 'after', 'inside', 'replace'
* if some tag other than 'position' is found in parent view
:raise Invalid ArchitectureError: if there is view type other than form, tree, calendar, search etc defined on the structure
"""
def distinct_field_get(self, cr, uid, field, value, args=[], offset=0,limit=2000)
def distinct_field_get(self, cr, uid, field, value, args=None, offset=0, limit=None):
if not args:
args = []
if field in self._inherit_fields:
return self.pool.get(self._inherit_fields[field][0]).distinct_field_get(cr, uid, field, value, args, offset, limit)
else:
return self._columns[field].search(cr, self, args, field, value, offset, limit, uid)
记录名字存取方法:name_get, name_search
def name_get(self, cr, uid, ids, context={})
def name_get(self, cr, user, ids, context=None):
"""Returns the preferred display value (text representation) for the records with the
given ``ids``. By default this will be the value of the ``name`` column, unless
the model implements a custom behavior.
Can sometimes be seen as the inverse function of :meth:`~.name_search`, but it is not
guaranteed to be.
:rtype: list(tuple)
:return: list of pairs ``(id,text_repr)`` for all records with the given ``ids``.
"""
def name_search(self, cr, uid, name='', args=[], operator='ilike',context={})
def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
"""Search for records that have a display name matching the given ``name`` pattern if compared
with the given ``operator``, while also matching the optional search domain (``args``).
This is used for example to provide suggestions based on a partial value for a relational
field.
Sometimes be seen as the inverse function of :meth:`~.name_get`, but it is not
guaranteed to be.
This method is equivalent to calling :meth:`~.search` with a search domain based on ``name``
and then :meth:`~.name_get` on the result of the search.
:param list args: optional search domain (see :meth:`~.search` for syntax),
specifying further restrictions
:param str operator: domain operator for matching the ``name`` pattern, such as ``'like'``
or ``'='``.
:param int limit: optional max number of records to return
:rtype: list
:return: list of pairs ``(id,text_repr)`` for all matching records.
"""