I am building an odoo14 module. The model has an image which, if the user didn't select any I want to generate a red image and set it to that. this is my current approach:
@api.model
def create(self, vals):
if not vals.get('plan'):
img = Image.new('RGB', (640, 480), '#FF0000')
vals['plan'] = img
res = super(Halle, self).create(vals)
return res
the image property looks like this
plan = fields.Image(string="Plan")
unfortunately, I get the following error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/http.py", line 640, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 316, in _handle_exception
raise exception.with_traceback(None) from new_cause
TypeError: 'Image' object is not subscriptable
id be very thankful if you know a solution or workaround for this problem. thank you very much.
You need to provide a base64 string representation of the file content. You can check the test_website_sale_image.py file for examples.
Try the following code:
@api.model
def create(self, vals):
if not vals.get('plan'):
f = io.BytesIO()
Image.new('RGB', (640, 480), '#FF0000').save(f, 'JPEG')
f.seek(0)
vals['plan'] = base64.b64encode(f.read())
res = super(SaleOrder, self).create(vals)
return res