How to load images from URL in Odoo 13

How to load images from URL in Odoo 13

2 minutes, 10 seconds Read

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'
})

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *