user3131595
user3131595

Reputation: 15

Odoo 10, To display Default Image

How to display a default image (saved in /my_module/static/img/car1.png) in a form ?

I have a binary field like this:

car_image =field.Binary(string="car1",default=??)

in xml, i gave like this:

Please help

Upvotes: 1

Views: 670

Answers (1)

KbiR
KbiR

Reputation: 4174

You can refer _get_default_image in res.partner.py

@api.model
def _get_default_image(self, partner_type, is_company, parent_id):
    if getattr(threading.currentThread(), 'testing', False) or self._context.get('install_mode'):
        return False

    colorize, img_path, image = False, False, False

    if partner_type in ['other'] and parent_id:
        parent_image = self.browse(parent_id).image
        image = parent_image and base64.b64decode(parent_image) or None

    if not image and partner_type == 'invoice':
        img_path = get_module_resource('base', 'static/src/img', 'money.png')
    elif not image and partner_type == 'delivery':
        img_path = get_module_resource('base', 'static/src/img', 'truck.png')
    elif not image and is_company:
        img_path = get_module_resource('base', 'static/src/img', 'company_image.png')
    elif not image:
        img_path = get_module_resource('base', 'static/src/img', 'avatar.png')
        colorize = True

    if img_path:
        with open(img_path, 'rb') as f:
            image = f.read()
    if image and colorize:
        image = tools.image_colorize(image)

    return tools.image_resize_image_big(base64.b64encode(image))

Create function

@api.model
def create(self, vals):
    if vals.get('website'):
        vals['website'] = self._clean_website(vals['website'])
    if vals.get('parent_id'):
        vals['company_name'] = False
    # compute default image in create, because computing gravatar in the onchange
    # cannot be easily performed if default images are in the way
    if not vals.get('image'):
        vals['image'] = self._get_default_image(vals.get('type'), vals.get('is_company'), vals.get('parent_id'))
    tools.image_resize_images(vals)
    partner = super(Partner, self).create(vals)
    partner._fields_sync(vals)
    partner._handle_first_contact_creation()
    return partner

Hope this will help you.

Upvotes: 1

Related Questions