Odoo 10-显示给定产品模板的图像 [英] Odoo 10 - Show image of a given product template

查看:95
本文介绍了Odoo 10-显示给定产品模板的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to extend product template form view to incorporate an additional field that will display photo of another related product template (so product_template id 10 displays in this additional field image of product_template 20)

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

I see that the image field is defined in the model as:

# 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.")

哪种方式定义此新字段?可以使用计算字段吗?有没有更简单的参考可以使用?

Which would be the way of defining this new field? Can a computation field be used? Is there any simpler reference that can be used?

推荐答案

在这里,我将介绍在自定义模型中定义Image文件并提供值的过程.

Here I am expalining the process of defining an Image fiedl in my custom model and giving values.

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'))

这篇关于Odoo 10-显示给定产品模板的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆