2017-08-11 57 views
0

我正在尝试扩展产品模板表单视图,以合并显示其他相关产品模板的照片的其他字段(因此product_template ID 10显示在product_template的其他字段图像中)Odoo 10 - 显示给定产品模板的图像

我看到像场在模型中定义为:

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

这将是确定这一新领域的方式是什么?可以使用计算字段吗?有没有更简单的参考可以使用?

回答

1

这里我将展示在我的自定义模型中定义图像fiedl并给出值的过程。

from odoo import models, api, tools 

class CustomModel(models.Model): 
    _name = "custom.model" #or your inherited model 
    # inherit if product.template and use the related fielf product id if needed 
    image = fields.Binary("Image", compute='_compute_image_vals') 

@api.depends('image') 
def _compute_image_vals(self): 
    self.image = self._get_default_image(self.product_id) 

@api.model 
def _get_default_image(self, product_id): 
    image = False 
    if product_id: 
     product_image = self.browse(product_id).image 
     image = product_image and product_image.decode('base64') or None 
     return tools.image_resize_image_big(image.encode('base64')) 
+0

只是提到他,因为我是用<字段名=“...”小工具=‘图像’类=‘oe_avatar’只读=‘真’/>在我只是在_get_default_image方法返回product_image形式。为什么要解码和编码?刚返回product_image有什么缺失吗? –

+0

这篇文章有一个小错误。只有分配给_name的现有模型的名称才能继承。 可能的定义是:_inherit和_name(继承),只有_inherit而不是_name(扩展名),_name和_inherit ** s **(委派)。 在odoo文档中查看[继承](https://www.odoo.com/documentation/10.0/reference/orm.html#inheritance-and-extension)。 – coreuter

+0

继承与自定义名称创建一个新表的权利? @coreuter –