This question has been flagged
5 Replies
165890 Views
def onchange_get_datas(self, cr, uid, ids, image_logo_attachment_id, context=None):
# import pdb;pdb.set_trace()
if image_logo_attachment_id:
image_logo_datas = image_logo_attachment_id.datas
else:
image_logo_datas = False

return {'value':{'image_logo_datas':image_logo_datas}}

This is my cod ^

and the fields are :

'image_logo_attachment_id': fields.many2one('ir.attachment', 'Image logo attachment',
help='Technical field to store image in filestore'),
'image_logo_datas': fields.related('image_logo_attachment_id', 'datas', type='binary',
string='Image logo datas', Class=".oe_avatar"),

Avatar
Discard

please provide us the xml view field definition to check the onchange arguments to provide you with a full answer

Author

<field name="image_logo_datas" nolabel="1" widget='image' class="oe_avatar" on_change="onchange_get_datas(image_logo_datas)"/>

Best Answer

Hi Ahmed

** Update: this is the original answer for use an onchange for the datas field from the attachment

The onchange need to be defined for the field image_logo_attachment_id in the view like:

<field name="image_logo_attachment_id" on_change="onchange_get_datas(image_logo_attachment_id)"/>

And the onchange definition in python need to be as follow:

def onchange_get_datas(self, cr, uid, ids, image_logo_attachment_id, context=None):
if image_logo_attachment_id:
image_logo_datas = self.pool.get('ir.attachment').browse(cr, uid, image_logo_attachment_id).datas
else:
image_logo_datas = False

return {'value':{'image_logo_datas':image_logo_datas}}

****

** Update: For using directly related fields to create an attachment:

class custom_class(models.Model):
    _name = 'custom.module'

    _columns = {
        'image_logo_attachment_id': fields.many2one(
            'ir.attachment', 
            string='Image logo attachment',
            help='Technical field to store image in filestore'
        ),
        'image_logo_datas': fields.related(
            'image_logo_attachment_id', 
            'datas', 
            type='binary',
            string='Image logo datas' 
        ),
        'image_logo_datas_fname': fields.related(
            'image_logo_attachment_id', 
            'datas_fname', 
            type='char',
            string='Image logo fname'
        )
    }

    def create(self, cr, uid, vals, context=None):
        if vals.get('image_logo_datas', False) and vals.get('image_logo_datas_fname', False):
            attach_id = self.pool.get('ir.attachment').create(cr, uid, {
                'name': vals.get('image_logo_datas_fname'),
                'datas_fname': vals.get('image_logo_datas_fname'),
                'datas': vals.get('image_logo_datas')
            }, context=context)
            vals['image_logo_attachment_id'] = attach_id
        res = super(custom_class, self).create(cr, uid, vals, context=context)
        return res
    
    def write(self, cr, uid, ids, vals, context=None):
        if vals.get('image_logo_datas', False) and vals.get('image_logo_datas_fname', False):
            for elem in self.browse(cr, uid, ids, context=context):
                if not elem.image_logo_attachment_id:
                    attach_id = self.pool.get('ir.attachment').create(cr, uid, {
                        'name': vals.get('image_logo_datas_fname'),
                        'datas_fname': vals.get('image_logo_datas_fname'),
                        'datas': vals.get('image_logo_datas')
                    }, context=context)
                    elem.write({'image_logo_attachment_id': attach_id})
        res = super(custom_class, self).write(cr, uid, ids, vals, context=context)
        return res

and use it like this in the view:

<field name="image_logo_datas" filename="image_logo_datas_fname" widget='image' class="oe_avatar"/>
<field name="image_logo_datas_fname" invisible="1"/>

For the future, make sure that you provide all the code that you have to make it better to fix it and use the same references and objects names. Provide better descriptions of the situation and what you are trying to do, don't let anything out.

That make it easy to answer questions.  

**

Avatar
Discard
Author

but i don't have this field in the view because i'm uploading the image from field 'image_logo_datas' (related field) ?

check the answer update

Best Answer

Hello,

It looks like field image_logo_attachment_id is a str and not record.

How are you passing variable image_logo_attachment_id to your function. Please share how you call the method onchange_get_datas.

Avatar
Discard
Author

<field name="image_logo_datas" nolabel="1" widget='image' class="oe_avatar" on_change="onchange_get_datas(image_logo_datas)"/>