Enable Dark Mode!
By: Sagarika

How to Load the Images from URL in Odoo 15

Technical Odoo 15

A business platform must use media such as images, videos or audio for convenient customer communication. Knowing this, Odoo supports the use of all kinds of media on its platform. There may be use cases where any media is not saved as an attachment. Using Odoo, it is possible to use media with or without linking to attachments.
Binary fields are used to store Images in Odoo Database. Currently, in Odoo, there is no feature to load an image from the URL. It is only possible to upload an already present image into the system. The basic definition of an image field in Odoo will look like the following.
from odoo import models
class PartnerImage(models.Model):
   _inherit = 'res.partner'
   img_customer = fields.Binary(string="Image", attachment=False, stored=True)
This will create a binary field img_customer in 'res.partner' model.This image will not be saved as an attachment as the Attachment attribute is false.
There may be some use cases in any business organisation where an image needs to be loaded from the image URL. It is not possible in the current system. But this can be achieved by following the steps described below.
First of all, create a new model which takes the URL of the image as a character field and saves the corresponding image in a binary field.
from odoo import fields, models
class Custom(models.Model):
   _name = "custom.custom"
   _description = "Custom"
   image_url = fields.Char(string="Image URL", required=True)
   image = fields.Binary(string="Image", store=True, attachment=False)
It is necessary to have a method that computes the binary value of the image by taking the URL as the input. But in such cases, it is not a good practice to write the compute method for each model where we need this feature. Creating a new Mixin class will be the best way to use the same method in every Model we use. The method to compute images from URLs can be defined in the same Mixin class. This method will be accessible in every method by simply inheriting the Mixin.
from odoo import api, fields, models
import requests
import logging
import base64
_logger = logging.getLogger(__name__)
class ImageFromURLMixin:
   def get_image_from_url(self, url):
       :return: Returns a base64 encoded string.
       data = ""
           # Python 2
           # data = requests.get(url.strip()).content.encode("base64").replace("\n", "")
           # Python 3
           data = base64.b64encode(requests.get(url.strip()).content).replace(b"\n", b"")
       except Exception as e:
           _logger.warning("Can’t load the image from URL %s" % url)
       return data
This  ImageFromURLMixin   Mixin includes the method  get_image_from_url() It takes the URL as the function parameter and returns the string in the proper format for saving in the database. To access this method in our custom model, we have to create the model by inheriting the Mixin class and Model class.Then the model definition needs to be rewritten as following.
from odoo import api, fields, models
class Custom(models.Model, ImageFromURLMixin):
   _name = "custom.custom"
   _description = "Custom"
   image_url = fields.Char(string="Image URL", required=True)
   image = fields.Binary(string="Image", compute="_compute_image", store=True,
   def _compute_image(self):
     for record in self:
           image = None
           if record.image_url:
               image = self.get_image_from_url(record.image_url)
           record.update({"image": image, })
A compute method is defined for computing the binary value of an image.
get_image_from_url()  function is called by passing the url stored in the 
field image_url .
Test it by creating a new record.It will compute and store the corresponding image in the form.

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


Leave a comment




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



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



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

Send Us A Message