1

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?

M.E.
  • 4,955
  • 4
  • 49
  • 128

1 Answers1

1

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'))
Hilar AK
  • 1,655
  • 13
  • 25
  • Just mention that as I was using in the form I just did return product_image in _get_default_image method. Why shall decode and encode be done? Is there anything missing with just returning product_image? – M.E. Aug 11 '17 at 11:21
  • There is a little mistake in the post. Inheritance won't work with just the name of an existing model assigned to _name. Possible definitions are: _inherit and _name (inheritance), only _inherit and not _name (extension), _name and _inherit**s** (delegation). Check out [inheritance](https://www.odoo.com/documentation/10.0/reference/orm.html#inheritance-and-extension) in the odoo documentation. – coreuter Aug 15 '17 at 15:23
  • inheritance with the custom name creates a new table right? @coreuter – Hilar AK Aug 16 '17 at 04:55
  • Yes, if you define your model with _name = 'new.name' and _inherit= 'existing.modelname', then a new table with all the fields of existing.modelname plus those you define in you model definition will be created. – coreuter Aug 16 '17 at 06:34
  • Then thats all I meant @coreuter – Hilar AK Aug 16 '17 at 06:40
  • Ok perfect. Your comment "#or your inherited model" next to _name confused me. Using just _name = 'exiting.model' won't inherit anything but overwrite an existing model – coreuter Aug 16 '17 at 06:52