跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. OpenERP在product中增加外部网络链接图片

OpenERP在product中增加外部网络链接图片

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
6 帖子 4 发布者 6.3k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • S 离线
    S 离线
    sage
    写于 最后由 编辑
    #1

    最近的一个项目要求在Product_Template中增加类似与HTML中<img src=”" />的形式的图片,效果如图:
    [img [检测到链接无效,已移除] /img]

    product_img_extra.py

    <br />from osv import osv, fields<br />import urllib2<br />import base64<br />class product_template_img(osv.osv):<br />&nbsp; &nbsp; _name = &#039;product.template&#039;<br />&nbsp; &nbsp; _inherit = &#039;product.template&#039;<br />&nbsp; &nbsp; _description = &#039;Product Extra Image&#039;<br /><br />&nbsp; &nbsp; def _get_image(self, cursor, user, ids, name, arg, context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; image = {}<br />&nbsp; &nbsp; &nbsp; &nbsp; opener = urllib2.build_opener()<br />&nbsp; &nbsp; &nbsp; &nbsp; res = self.read(cursor, user, ids, &#91;&#039;image_link&#039;])<br />&nbsp; &nbsp; &nbsp; &nbsp; image_link = res[0]&#91;&#039;image_link&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; if image_link:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pic = base64.encodestring(opener.open(image_link).read())<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for id in ids:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image[id] = pic<br />&nbsp; &nbsp; &nbsp; &nbsp; return image<br /><br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;image_link&#039; : fields.char(&#039;Image Link&#039;, size=180),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;image&#039; : fields.function(_get_image, method=True, string=&#039;Product Image&#039;, type=&#039;binary&#039;, store=False), <br />&nbsp; &nbsp; }<br /><br />product_template_img()<br />
    

     

    product_img_extra_view.xml

    <br />&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />&lt;openerp&gt;<br />	&lt;data&gt;<br />		&lt;record id=&quot;product_img_view&quot; model=&quot;ir.ui.view&quot;&gt;<br />			&lt;field name=&quot;name&quot;&gt;product.template.product.form&lt;/field&gt;<br />			&lt;field name=&quot;model&quot;&gt;product.template&lt;/field&gt;<br />			&lt;field name=&quot;type&quot;&gt;form&lt;/field&gt;<br />			&lt;field name=&quot;inherit_id&quot; ref=&quot;product.product_template_form_view&quot; /&gt;<br />			&lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;<br />				&lt;field name=&quot;name&quot; position=&quot;before&quot;&gt;<br />					&lt;field name=&quot;image&quot; widget=&quot;image&quot; nolabel=&quot;1&quot; img_width=&quot;168&quot; img_height=&quot;168&quot; colspan=&quot;4&quot;/&gt;<br />					&lt;field name=&quot;image_link&quot; colspan=&quot;4&quot;/&gt;<br />				&lt;/field&gt;<br />			&lt;/field&gt;<br />		&lt;/record&gt;<br />	&lt;/data&gt;<br />&lt;/openerp&gt;<br /><br />
    



    由于OpenERP的基本开发概念的其中一点是读取数据库动态生成界面.这时我就想到了使用fields.function来在界面生成时做一些特别的处理.

    product_img_extra.py中的_columns将对productTemplate增加image_link和image两个column.其中image_link用于保存图片链接的column,当界面生成时通过链接读取图片.image为显示图片的column,但由于store已设置为False所以不会直接保存到数据库中,而只会在界面生成时运行.

    因为需要读取外部链接,所以需要用到Python自带的urllib2模块,值得注意的是opener.open(image_link).read()获取回来的虽然已经是二进制数据,但仍需要使用base64.encodestring对其进行转码才可以被所识别.

    转载自:http://blog.sajolab.com/?p=52

    1 条回复 最后回复
    0
    • mrshellyM 离线
      mrshellyM 离线
      mrshelly
      写于 最后由 编辑
      #2

      不错... .赞一个先....

      1 条回复 最后回复
      0
      • O 离线
        O 离线
        oldrev
        写于 最后由 编辑
        #3

        很聪明的想法

        1 条回复 最后回复
        0
        • M 离线
          M 离线
          mihi
          写于 最后由 编辑
          #4

          思路相当的好,很受启发。

          1 条回复 最后回复
          0
          • M 离线
            M 离线
            mihi
            写于 最后由 编辑
            #5

            问楼主及其他大牛。
            openobject里边的方法是在服务器端执行还是客户端执行。
            如果是在服务器端执行,那么我需要在客户端执行一段代码,怎么办?

            1 条回复 最后回复
            0

            • 登录

            • 没有帐号? 注册

            • 登录或注册以进行搜索。
            • 第一个帖子
              最后一个帖子
            0
            • 版块
            • 标签
            • 热门
            • 用户
            • 群组