Enable Dark Mode!
how-to-load-images-from-url-in-odoo-13.png
By: Mohammed Shahil

How to load images from URL in Odoo 13

Technical Odoo 13

In Odoo, Images are stored in binary fields in databases. There is no option in Odoo to load images from the URL. We can only upload the image from the system.

For Example:-

from odoo import models
class PartnerImage(models.Model):
   _inherit = res.partner'    
partner_image = fields.Binary(string=Partner Image', attachment=False)

In the code above, we created a binary field called partner_image, which is stored on the res.partner model, not as an attachment. Which is set as attachment = False by default

So if you look at the table of the database we can see a column for the field of the partner_image. It will be in res_partner, in this situation. The contents contain a binary string of the image encoded in base64.

import requests
import base64
class CustomImage():
def load_image_from_url(self, url):
    data = base64.b64encode(requests.get(url.strip()).content).replace(b'\n', b'')
    return data

We have a simple load_image_from_url function in this mixin which takes in a URL pointing to an image and then returns the proper string that we can store in the database.

class CustomImageModel(models.Model, CustomImage):
_name = 'partner.image.model'
    
parner_image_url = fields.Char(string='Partner Image URL', required=True)
image = fields.Binary(string='Image', compute='_compute_image', store=True, attachment=False)
    
@api.depends('partner_image_url')
def _compute_image(self):
    for record in self:
        image = None
        if record.parner_image_url:
            image = self.load_image_from_url(record.partner_image_url)
        record.update({'image': image, })

Here our own custom model called image.custom.model is created here. You can see our CustomImage mixin is used here.

class CustomImageModel(models.Model, CustomImage):

We'll then add a field to save a single Partner URL

partner_image_url = fields.Char(string=Partner Image URL', required=True)

Rather than manipulating the image with binary strings to display the image, or overriding the write function we can construct a simple compute field that will convert the image URL field automatically as it changes. Our mixer can use this compute feature to clean up the code.

binary_image = fields.Binary(string='Image', compute='_compute_binary_image', store=True, attachment=False)
    
@api.depends('partner_image_url')
def _compute_binary_image(self):
   """
   Automatically Computes the Binary image per database record from the `partner_image_url`.    
   :return: Returns NoneType
   """
   for record in self:
      image = None
      if record.partner_image_url:
         image = self.load_image_from_url(record.partner_image_url)
      record.update({'image': image, })

Use this code to create or change a record: 

object = self.env['partner.image.model'].create({
   'partner_image_url': 'URL of the image'
})


If you need any assistance in odoo, we are online, please chat with us.



1
Comments

Ron

Does this only work in Odoo 13? I tried this in odoo 12 and seems like it puts binary in field but gives me can not display image notification ? What am I missing

30/04/2020

-

7:42AM



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message