Odoo Help

0

How to set a default for binary field?

By
Juan Formoso
on 2/23/15, 4:48 AM 3,276 views

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?

0
ashokd
On 10/4/15, 12:36 PM

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 

}

0

Hi, your code is fine. If you get this


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

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.

Juan Formoso
on 2/26/15, 10:11 AM

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]}"

Juan Formoso
on 2/26/15, 10:13 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 2/23/15, 4:48 AM
Seen: 3276 times
Last updated: 12/22/15, 9:22 PM