关于图片
- 
Vincent 对于图片自定义存储的东西.我写了个demo shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:56:15 
 1 首先在对象里还是要定义图片字段(至于不定义的 暂时没有去研究)
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:56:17
 'image': fields.binary('Image'),
 }
 Jack(104144648) 17:56:21
 论坛?shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:56:44 
 2 定义 creat 方法,重写对象的 create 方法.
 def create(self, cr, uid, vals, context={}):
 tmpImage = ''
 if vals['image']:
 tmpImage = vals['image']
 del vals['image']employee_id = super(cbgd_hr_employee, self).create(cr, uid, vals, context) if not (tmpImage == ''): fp = open('e:\tmppic\employee_%010d.jpg' % employee_id, 'wb') fp.write(base64.decodestring(tmpImage)) fp.close() return employee_idJack(104144648) 17:57:30 
 这个还好理解,我不理解是怎么打开一个文件,在界面上怎么定义shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:58:21 
 这段代码很容易读懂.
 前面把 "保存"按钮 发过来的数据中的 Image 数据 保存到一个临时变量中 tmpImage然后 调用父方法,创建 对象数据. 得到创建对象的ID后. 
 根据 ID 生成一个文件名. 并保存到指定的地点. 我暂时用的文件存储. 你可以写到其它的地方.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:58:29
 然后返回 生成对象的ID
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:58:34
 这是保存的操作.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 17:59:01
 3 定义 read 方法,重写对象的 read 方法.
 def read(self, cr, uid, ids, fields=None, context={}):
 result = super(cbgd_hr_employee, self).read(cr, uid, ids, fields, context)
 if not isinstance(result,list):
 result=[result]for res in result: try: fp = open('e:\tmppic\employee_%010d.jpg' % res['id'], 'rb') except IOError,ex: res['image'] = False continue tmpContent = base64.encodestring(fp.read()) res['image'] = tmpContent return resultshelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:00:33 
 这段代码也不难理解.1 调用原 read 方法,取出保存在数据库中的数据. 然后 在返回的数据中 加入 image 字段的内容. 只是注意, 对于二进制数据.都需要 base64 进行编码,解码. 最后返回修整后的 result 数据. 
 Jack(104144648) 18:00:59
 对象的方法。。
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:01:18
 To Jack 关于图片数据 在客户端( Client, webClient) 中如何显示. 这个不属于模块的内容的.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:01:33
 服务器端只管返回字段的 base64 数据即可.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:02:07
 嗯. 象这种个性化的字段调整, 就是需要重写对象的方法定义.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:03:15
 对于图片的修改部分.也差不多.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:03:34
 就是重写对象的 write 方法.
 Jack(104144648) 18:04:36
 不要修改了这是不需要的,要改也不用openerp干,除非加个什么商标什么的
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:05:26
 不是.
 shelly→水<[email:2almbx3o][email protected][/email:2almbx3o]> 18:05:48
 对于产品图片.这些, 有时候 使用文件系统比存到数据库中更方便.
 Jack(104144648) 18:06:20
 现在还有一个问题这么在界面定义一个打开文件的操作
- 
对于不在数据库中生成垃圾字段的做法. 1 在对象 fields 定义时.字段使用 function 类型 字段,并指明字段的存取 function 名. 
 例:
 'datas': fields.function(_data_get,method=True,fnct_inv=_data_set,string='File Content',type="binary"),
 'datas_fname': fields.char('File Name', size=100, help='照片文件名'),指定了 _data_get 为获取该字段的function _data_set 为保存该字段的 function datas_fname 用于临时保存 文件名. 
- 
2 定义 _data_get 以及 _data_set 方法: def _data_get(self, cr, uid, ids, name, arg, context): result = {} for id in map(str,ids): try: value = file('e:\tmppic\employee_%010d.jpg' % int(id), 'rb').read() result[int(id)] = base64.encodestring(value) except: result[int(id)]='' return result def _data_set(self, cr, obj, id, name, value, uid=None, context={}): if not value: return True try: fp = file('e:\tmppic\employee_%010d.jpg' % int(id),'wb') v = base64.decodestring(value) fp.write(v) fp.close() return True except Exception,e : raise except_orm(_('Error!'), str(e))两段代码都很简单就不过多写注释了. 代码基本上参阅了 document 模块的相关部分. 
- 
我按照这里写了一个一样的模块,发生了错误[code] 
 Environment Information :
 System : Windows-XP-5.1.2600-SP2
 OS Name : nt
 Operating System Release : XP
 Operating System Version : 5.1.2600
 Operating System Architecture : 32bit
 Operating System Locale : zh_CN.cp936
 Python Version : 2.5.2
 OpenERP-Client Version : 5.0.6
 Last revision No. & ID :Bazaar Package not Found !Traceback (most recent call last):
 File "netsvc.pyo", line 244, in dispatch
 File "netsvc.pyo", line 73, in call
 File "serviceweb_services.pyo", line 583, in execute
 File "osvosv.pyo", line 59, in wrapper
 File "osvosv.pyo", line 118, in execute
 File "osvosv.pyo", line 110, in execute_cr
 File "osvorm.pyo", line 2084, in read
 File "osvorm.pyo", line 2195, in _read_flat
 File "osvfields.pyo", line 663, in get
 AttributeError: 'str' object has no attribute 'items'
 [/code]我也是使用了function的fields,
 写了_data_get,_data_set方法经过Mrshelly指导,已解决,原来是自己的return不是返回一个数组 [[i] 本帖最后由 popkar77 于 2009-10-30 16:11 编辑 [/i]] 
- 
终于搞掂了这个问题。谢谢Mrshelly的帮助 [[i] 本帖最后由 vincent@mrsf 于 2009-11-4 11:29 编辑 [/i]] 
- 
扩展了一下,如果想要显示网络图片 
 只需要修改一下 _data_get 函数,
 先导入 urllib2包获取图片代码 req = urllib2.Request('http://www.jpeg.org.cn/photo/2006/1111/smsj/t/smsj-001.jpg') #图片地址,暂时只测试了jpg格式的图片 response = urllib2.urlopen(req) value = response.read() result[int(id)]= base64.encodestring(value)result就是经过base64加密了的图片数据 
