Odoo 10 - 显示给定产品模板的图像

时间:2017-08-11 03:47:54

标签: openerp

我正在尝试扩展产品模板表单视图以合并另一个字段,该字段将显示另一个相关产品模板的照片(因此product_template id 10显示在product_template 20的附加字段图像中)

我看到图像字段在模型中定义为:

# 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 个答案:

答案 0 :(得分:1)

这里我正在考虑在我的自定义模型中定义Image 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'))