This question has been flagged
1 Reply
9014 Views

In my custom module there is a binary field for images. I loaded an image in that. I have another HTML field for image preview.  My preview field image need to change when I add css styles. For example, if I add height and width as fields and if I enter some values in that fields, Image(Html field) styles needs to change corresponding to height and width.

Code:

​​class ImagePreview(models.Model):
_name = 'image.preview'

images = fields.Binary(string='images')

width = fields.Float(string='Width')
height = fields.Float(string='Height')
preview = fields.Html(string="Preview", compute='img')

def img(self):
    with io.BytesIO(base64.b64decode(bytes(self.images, 'utf-8'))) as f:
        photo = f.read()
    htmlString = "<div><img src='" + str(photo) + "'></div>"
    self.preview = htmlString
    return

XML

   <record id="preview_form_view" model="ir.ui.view">
<field name="name">form</field>
<field name="model">image.preview</field>
<field name="arch" type="xml">
<form string="Discount Ribbons">
<sheet>
<group>
<group>
<field name="images" widget="image" class="oe_avatar" string="Image"/>
</group>
<group>
<div>
<field name="preview" widget="Html"/>
</div>
</group>
</group>
<group>
    <field name="width" string = 'Width'/>
<field name="height" string = 'Height'/>
</group>
            </sheet>
</form>
</field>
</record>
Avatar
Discard