Skip to Content
Menu
This question has been flagged

I'm working on v7. I'm trying to set a default image "my_image.png" for a binary field. If the user opens the form, "my_image.png" must be visible until the user changes it.

My code:

def _get_default_signature(self, cr, uid, context=None):
    import base64
    f = open('static/src/img/my_image.png')
    image = base64.encodestring(f.read())
    f.close()
    return image

_columns = {
    'signature': fields.binary('Signature'),
}

_defaults = {
    'signature': _get_default_signature,
}

I'm not able to show "my_image.png" as the default value for the field signature. Can anyone help me here, please?

Avatar
Discard
Best Answer

use ir.attachement

first upload a file in attachments. goto  settings ->    Technical -> Data structure -> create record -> search with name

def _get_default_welcome_letter(self,cr,uid,context=None):

        attach_obj = self.pool.get('ir.attachment')

        attach_data_ids=attach_obj.search(cr,uid,[('name','=','welcome_template.docx')])

        attach_data = attach_obj.browse(cr,uid,attach_data_ids)

        return attach_data.datas


_columns = {

'welcome_pack1_download' : fields.binary('Welcome Letter'),

}

_defaults={

'welcome_pack1_download' : _get_default_welcome_letter 

}

Avatar
Discard
Best Answer

Hi, your code is fine. If you get this


Just add widget=image in xml file.
<field name="signature" widget="image" />

Avatar
Discard
Author

Thank you very much for your answer, Dhinesh! However, my XML was ... I tried to remove the class and the options to leave the field as you told me, but when I open the form, I still see the default picture of the camera, like the rest of the binary fields.

Author

Sorry, but I always forget that if I write some code symbols in a comment, the line just disappears. I tried to tell you that my XML code was field name="signature" widget="image" class="oe_left oe_avatar" options="{'size': [400, 120]}"

Related Posts Replies Views Activity
1
Dec 22
4480
2
Feb 23
5847
0
Jul 16
3625
1
Mar 16
2002
1
Oct 18
6325