Published 9th May 2020
Please follow the below three simple steps to set default image for a binary field.
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.....................
DEFAULT_IMG = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAA.....................'This will set the default image :)
class MyModel(models.Model):
_name = 'my.model'
field_binary = fields.Binary(default=DEFAULT_IMG)
Eg .. /my_module/static/img/my_image.png
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())
Published 9th May 2020
Please fill all the details and give your valuable comments
No comments yet. Be the first to share your thoughts!