Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

    Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

    开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

    如果您登录系统碰到问题,请在微信公众号留言:

    关于图片

    Odoo 开发与实施交流
    5
    10
    7156
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      simon last edited by

      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_id
      

      Jack(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 result
      

      shelly→水<[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 Reply Last reply Reply Quote 0
      • P
        popkar77 last edited by

        支持!!:D

        1 Reply Last reply Reply Quote 0
        • mrshelly
          mrshelly last edited by

          对于不在数据库中生成垃圾字段的做法.

          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 用于临时保存 文件名.

          1 Reply Last reply Reply Quote 0
          • mrshelly
            mrshelly last edited by

            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 模块的相关部分.

            1 Reply Last reply Reply Quote 0
            • O
              oldrev last edited by

              搞不懂, attachment 是可以选择数据库还是文件的啊

              1 Reply Last reply Reply Quote 0
              • P
                popkar77 last edited by

                我按照这里写了一个一样的模块,发生了错误[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]]

                1 Reply Last reply Reply Quote 0
                • V
                  vincentmrsf last edited by

                  终于搞掂了这个问题。谢谢Mrshelly的帮助

                  [[i] 本帖最后由 [email protected] 于 2009-11-4 11:29 编辑 [/i]]

                  1 Reply Last reply Reply Quote 0
                  • V
                    vincentmrsf last edited by

                    扩展了一下,如果想要显示网络图片
                    只需要修改一下 _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加密了的图片数据

                    1 Reply Last reply Reply Quote 0
                    • mrshelly
                      mrshelly last edited by

                      呵呵.

                      如果要 _data_set 哩...
                      看来你要使用 urllib 进行远程图片上传了.

                      如果你的 WEB Server 支持 webdav 还好...

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post