Solution 1 (Direct assigning Base64)


Please follow the below three simple steps to set default image for a binary field.


Step 1 : Convert  image file to base64



        CONVERT ONLINE 


You can convert image file online. Example of Converted code ..

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAA.....................

Step 2 : Remove the prifix 'data:image/png;base64,'


Example code after removing.

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAA.....................

Step 3 : Assign Default Image

DEFAULT_IMG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAA.....................'

class MyModel(models.Model):
_name = 'my.model'

field_binary = fields.Binary(default=DEFAULT_IMG)
This will set the default image :)


Solution 2 (Include image file into the module)


Step 1 : Copy file into module.


Eg ..      /my_module/static/img/my_image.png


Step 2 : Set Default


import base64
from odoo import models, fields
from odoo import modules


def get_default_img():
with open(modules.get_module_resource('my_module', 'static/img', 'my_image.png'),
 'rb') as f:
return base64.b64encode(f.read())

class MyModel(models.Model):
_name = 'my.model'

field_binary = fields.Binary(default=get_default_img())

Hope this will help you  ...
Thanks ..!!

If you wish to have the latest updates from us, you may follow us on FACEBOOKTWITTERYOUTUBE etc.