2017-04-25 95 views
2

我需要关于odoo中的错误的帮助。 我已经为odoo中的图像创建了模型和xml。如何在odoo中创建和显示图像

这是我的模型

class Test(osv.osv): 
_name = "digital.test" 
_description = "Test" 

_columns = { 
    'Servicename': fields.char('Service Name'), 
    'prodescription': fields.html('Product description'), 
    'Subservicename': fields.char('Sub-Service Name'), 
    'subprodescription': fields.html('Product Desc.'), 
} 

# image: all image fields are base64 encoded and PIL-supported 
image = openerp.fields.Binary("Photo", attachment=True, 
    help="This field holds the image used as photo for the test, limited to 1024x1024px.") 
image_medium = openerp.fields.Binary("Medium-sized photo", attachment=True, 
    help="Medium-sized photo of the test. It is automatically "\ 
     "resized as a 128x128px image, with aspect ratio preserved. "\ 
     "Use this field in form views or some kanban views.") 
image_small = openerp.fields.Binary("Small-sized photo", attachment=True, 
    help="Small-sized photo of the test. It is automatically "\ 
     "resized as a 64x64px image, with aspect ratio preserved. "\ 
     "Use this field anywhere a small image is required.") 

def _get_default_image(self, cr, uid, context=None): 
    image_path = get_module_resource('mymodule', 'static/src/img', 'default_image.png') 
    return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64')) 

defaults = { 
    'active': 1, 
    'image': _get_default_image, 
    'color': 0, 
} 

@api.model 
def create(self, vals): 
    tools.image_resize_images(vals) 
    return super(digital.test, self).create(vals) 

,这我的XML

 <record id="view_test_form" model="ir.ui.view"> 
     <field name="name">digital.test.form</field> 
     <field name="model">digital.test</field> 
     <field name="arch" type="xml"> 
      <form string="Test"> 
       <sheet> 
        <field name="image" widget='image' class="oe_avatar" options='{"preview_image":"image_medium"}'/> 
        <div class="oe_title"> 
         <label for="Service Name" class="oe_edit_only"/> 
         <h1> 
          <field name="Servicename" placeholder="Service Name"/> 
         </h1> 
        </div> 
        <notebook> 
         <page string="Product Description"> 
          <group string="Service Name"> 
           <field name="prodescription" type="html"/> 
          </group> 
          <group string="Sub-Service Name"> 
            <field name="Subservicename"/> 
            <field name="subprodescription" widget="html"/> 
          </group> 
         </page> 
        </notebook> 
       </sheet> 
      </form> 
     </field> 
    </record> 

但是,我的代码是,当我创建这样 enter image description here

我可以保存数据的新数据 错误,当我删除@ api.model def创建 但是,我的图像无法保存。

谢谢

回答

0

你的类继承了错误sintaxis,它应该是:

@api.model 
def create(self, vals): 
    tools.image_resize_images(vals) 
    return super(Test, self).create(vals) 
+0

是的,我错了sintax。 Ty为你的答案。 –