Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
5 Trả lời
168231 Lượt xem
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"),

Ảnh đại diện
Huỷ bỏ

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

Tác giả

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

Câu trả lời hay nhất

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.  

**

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 2 20
1560
0
thg 10 15
3547
2
thg 5 15
4536
1
thg 3 15
3525
9
thg 7 25
17715