Enable Dark Mode!
how-to-load-the-images-from-url-in-odoo-16.jpg
By: Sruthi M Kumar

How to Load the Images from URL in Odoo 16

Technical Odoo 16

For easy client communication, a business platform should incorporate media like voice, videos, or photographs. Odoo is aware of this and encourages the use of all media on its platform.

In some situations, no media is saved as an attachment. Using media with or without linking to attachments is possible with Odoo.

In the database, Odoo stores photos using binary fields. The ability to load a picture from a URL is not yet available in Odoo. Only already existing images can be uploaded to the system. In Odoo, an image field has the following fundamental definition:

from odoo import models
class PartnerImage(models.Model):
   _inherit = 'res.partner'
   img_customer = fields.Binary(string="Image", attachment=False, stored=True)
img_customer will be created as a binary field in the "res. partner" model as a result.
Due to the false value of the attachment attribute, this image will not be saved as an attachment.
Any business organization may occasionally need to load a picture from an image URL.
That is not possible under the existing setup. But it is achievable by doing the things listed below.
We must first develop a new model that stores the corresponding image in a binary field and accepts the image's URL as a character field.
 class ImageUrl(models.Model, ImageFromURLMixin):   _name = 'custom.image'   image_url = fields.Char(string="Image URL", required=True)   create_image = fields.Binary(string="Image", compute='_compute_image', readonly=False, store=True)
A procedure that determines the image's binary value using the URL as input is required.
However, under these circumstances, writing a computing method for each model where we require this functionality is not a smart practice. To utilize the same method across all models, creating a new mixin class would be a wonderful idea. The same mixin class can define a method to compute images from URLs. By inheriting mixin, all methods can access this method.
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 = ""
      try:
          # 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)
          logging.exception(e)
      return data
The method get_image_fromurl() of this “ImageFromURLMixin” Mixin accepts the URL function as a parameter and returns a string with the appropriate format to be saved in the database.
We must create the model by inheriting from the model class and mixin class in order to utilize this method in our custom model. It is then necessary to rewrite the model definition as follows.
from odoo import api, fields, models
class ImageUrl(models.Model, ImageFromURLMixin):
   _name = 'custom.image'
   image_url = fields.Char(string="Image URL", required=True)
   create_image = fields.Binary(string="Image", compute='_compute_image', readonly=False, store=True) 
@api.depends("image_url")
   def _compute_image(self):
       for record in self:
           image = None
           if record.image_url:
               image = self.get_image_from_url(record.image_url)
               self.check_access_rule(image)
           record.update({"create_image": image, })
A computing method is defined to calculate the binary value of an image. The get_image_from_url() function is called by passing the URL stored in the image_url field.
Check it by creating a new record in odoo16. It will calculate and store the corresponding image in the form.

how-to-load-the-images-from-url-in-odoo-16-cybrosys


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



0
Comments



Leave a comment

 


whatsapp
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