Odoo Help

Welcome!

This community 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.

1

Using filesystem for binary field

By
YannickB
on 2/26/14, 11:38 AM 4,422 views

Hello everyone,

I need to create a binary field which will be stored in the filesystem. I already set my ir_attachment.location and it works fine.

Here is the code I used :

def _get_binary_filesystem(self, cr, uid, ids, name, arg, context=None):
    """ Display the binary from ir.attachment, if already exist """
    res = {}
    attachment_obj = self.pool.get('ir.attachment')

    for record in self.browse(cr, uid, ids, context=context):
        res[record.id] = False
        attachment_ids = attachment_obj.search(cr, uid, [('res_model','=',self._name),('res_id','=',record.id),('binary_field','=',name)], context=context)
        import logging
        _logger = logging.getLogger(__name__)
        _logger.info('res %s', attachment_ids)
        if attachment_ids:
            img  = attachment_obj.browse(cr, uid, attachment_ids, context=context)[0].datas
            _logger.info('res %s', img)
            res[record.id] = img
    return res

def _set_binary_filesystem(self, cr, uid, id, name, value, arg, context=None):
    """ Create or update the binary in ir.attachment when we save the record """
    attachment_obj = self.pool.get('ir.attachment')

    attachment_ids = attachment_obj.search(cr, uid, [('res_model','=',self._name),('res_id','=',id),('binary_field','=',name)], context=context)
    if value:
        if attachment_ids:
            attachment_obj.write(cr, uid, attachment_ids, {'datas': value}, context=context)
        else:
            attachment_obj.create(cr, uid, {'res_model': self._name, 'res_id': id, 'name': 'Marketplace picture', 'binary_field': name, 'datas': value, 'datas_fname':'picture.jpg'}, context=context)
    else:
        attachment_obj.unlink(cr, uid, attachment_ids, context=context)

_columns = {
    'picture': fields.function(_get_binary_filesystem, fnct_inv=_set_binary_filesystem, type='binary', string='Picture'),
}

View :

<field name="picture" widget="image" class="oe_left oe_avatar" options="{"preview_image": "image_medium", "size": [90, 90]}"/>

The set part works fine, when I check on ir.attachment I can see it and download the picture without problem. But, when I go in my object, the picture is still empty and seems to not be loaded. I have the logs :

2014-02-26 15:25:36,901 48785 INFO wezer_dev openerp.addons.marketplace.marketplace: res [3]

2014-02-26 15:25:36,904 48785 INFO wezer_dev openerp.addons.marketplace.marketplace: res 63.73 Kb

If you want the whole code, you can see it line 170 in http:// bazaar.launchpad.net/~yannick-buron/+junk/openerp-marketplace/view/head:/marketplace/marketplace.py.

Thank you for you help.

0
Cyril Sester
On 8/28/14, 6:29 AM

Hi Yannick,

You made everything fine. The only mistake is to have

"preview_image": "image_medium"

in the view field option. Try without it, works for me.

 

Regards,

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/26/14, 11:38 AM
Seen: 4422 times
Last updated: 1/12/16, 8:41 PM