This question has been flagged
1 Reply
10266 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.

Avatar
Discard
Best Answer

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,

Avatar
Discard