Skip to Content
Menu
This question has been flagged
2 Replies
13105 Views

Hi everyone,

I have a field of type image in Odoo 12, declared as:

image = fields.Binary("Image", attachment=True,)

On the form view I added the code for the user to choose an image.

What I need to do is, on both create and write methods, check if there is an image attached to the field and if not, set a default image to that field.

What I have tried (for write method as an example):

@api.model
def write(self, vals, context=None
        if not vals.get(self.image, False):
        get_image = "/mymodule/static/img/noimage.jpg"
        vals['image'] = get_image
return super(mymodule_model, self).write(vals)

Using this code i get the error: AttributeError: 'list' object has no attribute 'get'

also tried..

    @api.model
    def write(self, vals, contect=None):
        if (self.image == False):
            get_imae = "/mymodule/static/img/noimage.jpg"
            vals['image'] = get_image
     return super(mymodule_model, self).write(vals)

...and does not work either!

How can I test if the image field is empty and if so, have Odoo to save a default image to the database.

Thank you in advance

Regards

Avatar
Discard
Best Answer

Hello 

try with below code, this will helps you.

from odoo import api, fields, models,  tools
from odoo.modules import get_module_resource
import base64

class your_class(models.Model):
_name = 'your.class'
   
@api.model
    def _get_default_image_value(self):
        image = False
        img_path = get_module_resource('your_modulename', 'static/img', 'avatar.png') # your default image path
        if img_path:
            with open(img_path, 'rb') as f: # read the image from the path
                image = f.read()
        if image: # if the file type is .jpg then you don't need this whole if condition.
            image = tools.image_colorize(image)
        return tools.image_resize_image_big(base64.b64encode(image))
   
@api.model
    def create(self, vals):
        if not vals.get('image'):
            vals['image'] = self._get_default_image_value()
        return super(your_class, self).create(vals)
   
@api.multi
    def write(self, vals):
        res = super(your_class, self).write(vals)
        if not self.image:
            self.image = self._get_default_image_value()
        return res

Avatar
Discard
Author

Thank you @Mitul

I had to remove the line "image = tools.image_colorize(image)". Using this line, Odoo generates an error: on the "image = tools.image_colorize(image)". The error is: "ValueError: bad transparency mask".

I think it's related to the fact that I am using a .jpg and not a .png image file.

Can you please comment your code on "_get_default_image_value" so I can understand what exactly it does?

Thank you in advance. +1 for you.

i have update my answer.

Author

Great @Mitul. Thank you once again

Best Answer

check this code

if not vals.get('image', False):

or

if self.image == False:


Avatar
Discard
Related Posts Replies Views Activity
3
Oct 23
5974
1
Sep 23
1965
1
May 23
1001
2
Apr 23
1375
1
Mar 23
996