本文共 4465 字,大约阅读时间需要 14 分钟。
OpenERP作为一个强大的ERP系统,提供了丰富的字段类型来满足不同业务需求。这些字段类型可以分为基础类型、复杂类型和关系类型,以下是每种类型的详细说明。
Char/Text: 字符型字段。char通常用于固定长度字符串,如name字段,长度由size属性定义。text则没有长度限制,适用于需要较长文本的场景。
Boolean: 布尔型字段,值为true或false。
Integer/Float: 数字类型。integer用于整数值,float用于浮点数值,例如rate字段,可定义为digits=(12,6),表示整数部分和小数部分的位数。
Date/DateTime: 日期和日期时间类型。OpenERP支持标准的日期和时间格式,用户可以根据需求选择合适的日期控件。
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)
float。True表示为对象方法,False为全局函数。False时结果仅用于计算,True时结果会被存储。注意事项:store=True意味着每次读取该字段都会执行函数,结果会被存储到数据库中,便于DBA查看和其他系统访问。fcnt_inv用于写入字段值,fcnt_search定义了搜索行为,obj指定关联对象,method=True时需提供对象方法。
Selection: 下拉框类型,定义一个选项列表。例如:
'state': fields.selection((('n','Unconfirmed'),('c','Confirmed')),'State', required=True)这表示state字段可选n或c,并强制填写。
One2One/Many2One: 一对一和多对一关系。one2one在V5.0及以上已被many2one取代,建议使用many2one。many2one用于多对一关系,定义格式为:
fields.many2one('res.partner.address', 'partner_id', 'Contacts') 'cascade'或'null',默认为'null'。One2Many/Many2Many: 一对多和多对多关系。one2many定义为:
'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')OpenERP会自动创建关联表res_partner_category_rel,包含关联字段partner_id和category_id。
Reference: 引用型字段,表示该字段引用另一个对象的记录。例如:
'ref': fields.reference('Document Ref 2', selection=_links_get, size=128)引用值的格式为(object_name, ID),如('product.product',3)表示引用对象product.product中ID为3的记录。
Related: 关联字段,引用其他字段或对象的字段。定义格式为:
'city': fields.related('address','city',type='char', string='City')related字段通过指定的关系字段访问关联对象的字段。
Property: 属性字段,通过ir.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")属性字段不会在数据库中创建新的字段,而是通过ir.property关联到目标对象的字段。
change_default: 是否可以根据该字段的值设定其他字段的缺省值,默认False。
readonly: 是否为只读字段,默认False。
required: 是否为必填字段,默认False。
states: 定义特定状态生效的属性,格式为{'name_of_the_state': list_of_attributes}。
string: 字段显示名。
translate: 是否可以翻译字段值,默认False。
size: 字段长度。
priority: 字段优先级。
domain: 域条件,用于过滤关联表记录。
invisible: 是否可见,默认True。
selection: 只用于reference字段类型,定义下拉框选项。
OpenERP的核心对象继承自osv.osv,提供了多种预定义方法:
create: 插入新记录。
def create(self, cr, uid, vals, context={})返回新记录的ID。
search: 查询符合条件的记录。
def search(self, cr, uid, args, offset=0, limit=2000)
返回符合条件的记录ID列表。
read: 读取指定字段的值。
def read(self, cr, uid, ids, fields=None, context={})返回读取结果的字典列表。
browse: 浏览对象及其关联对象。
def browse(self, cr, uid, select, offset=0, limit=2000)
返回对象或对象列表,便于直接访问字段和方法。
write: 保存字段值。
def write(self, cr, uid, ids, vals, context={})返回True或抛出异常。
unlink: 删除记录。
def unlink(self, cr, uid, ids)
返回True或抛出异常。
default_get: 复位字段缺省值。
def default_get(self, cr, uid, fields, form=None, reference=None)
返回缺省值字典。
default_set: 重置字段缺省值。
def default_set(self, cr, uid, field, value, for_user=False)
修改缺省值。
fields_get: 获取字段定义。
def fields_get(self, cr, uid, fields=None, context={})返回字段定义信息。
fields_view_get: 获取视图定义。
def fields_view_get(self, cr, uid, view_id=None, view_type='form',context={})name_get: 获取记录名称。
def name_get(self, cr, uid, ids, context={})返回记录名称列表。
name_search: 搜索记录名称。
def name_search(self, cr, uid, name='', args=[], operator='ilike',context={})perm_read: 权限读取。
def perm_read(self, cr, uid, ids)
检查用户权限是否允许读取记录。
perm_write: 权限写入。
def perm_write(self, cr, uid, ids, fields)
以下是一个简单的OpenERP对象开发示例,创建一个基本的联系人模型:
# res_partner.pyclass res_partner(osv.osv): _name = 'res.partner' _description = '客户信息' def create(self, cr, uid, vals, context=None): vals.update({ 'user_id': uid, 'state': 'potential' }) return super(res_partner, self).create(cr, uid, vals, context=context) def write(self, cr, uid, ids, vals, context=None): for id in ids: partner = self.browse(cr, uid, id) partner.state = 'active' # 更新状态为活跃 return True def unlink(self, cr, uid, ids): for id in ids: self.browse(cr, uid, id).delete_record(cr, uid) return True def delete_record(self, cr, uid, context=None): # 定义删除逻辑 return True 上述代码仅展示了部分方法,实际开发中需要根据具体需求扩展字段和方法。
转载地址:http://awpfk.baihongyu.com/