Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
14248 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Autor

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.

Autor

Great @Mitul. Thank you once again

Najlepsza odpowiedź

check this code

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

or

if self.image == False:


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
cze 25
2697
3
paź 23
8813
1
wrz 23
3328
1
maj 23
2270
2
kwi 23
2825