Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
5 Răspunsuri
167659 Vizualizări
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"),

Imagine profil
Abandonează

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

Autor

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

Cel mai bun răspuns

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.  

**

Imagine profil
Abandonează
Autor

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

Cel mai bun răspuns

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.

Imagine profil
Abandonează
Autor

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

Related Posts Răspunsuri Vizualizări Activitate
1
feb. 20
1560
0
oct. 15
3025
2
mai 15
3965
1
mar. 15
2987
9
dec. 23
17233