This question has been flagged
4 Replies
5486 Views

hi all. I created a module. I used field function onchange filed. so it does not change . Can you help me?

 

Code .py

class danh_sach_nv(osv.Model):
    _name = 'danh.sach.nv'
    _rec_name = 'ma_nv'
    
    def onchange_luong(self, cr, uid, ids, ma_nv, context=None):
        res = {}
        if ma_nv:
            obj = self.pool.get('dieu.chinh').browse(cr, uid, ma_nv)
            res['luong_ct'] = obj.luong_dc
            
            return {'value': res}
    _columns = {
                
               'ma_nv':fields.char('Mã NV',size=30),

               'luong_ct':fields.char('Lương chính thức',size = 20),

}

 

class dieu_chinh(osv.Model):
    _name = 'dieu.chinh'
    _columns = {
                'ma_nv':fields.many2one('danh.sach.nv', 'Mã NV'),
                'ten_nv':fields.related('ma_nv', 'ten_nv', type ='char', size = 50, string = 'Tên nhân viên'),
                'luong_ct':fields.related('ma_nv', 'luong_ct', type ='char', size = 50, string = 'Tên nhân viên'),
                'luong_dc':fields.char('Lương điều chỉnh', size = 20),
                'ly_do_dc': fields.char('Lý do điều chỉnh', size = 30),
                
                }

 

.xml 

<record model= 'ir.ui.view' id ='danh_sach_nv_form'>
            <field name ='name'>danh.sach.nv.form</field>
            <field name ='model'>danh.sach.nv</field>
            <field name = 'arch' type = 'xml'>
                <form string = 'Nhân viên' version = '7.0'>

                        <group>
   
                           <field name = 'ma_nv' on_change = 'onchange_luong(ma_nv)'/>

                             <field name = 'luong_ct'/>

                         </group>

I dont know What I make mistakes?

Can you help me?

 

Avatar
Discard
Best Answer

Hi,

No need to change your code. Change the field type of field 'ma_nv' from char to many2one to object 'dieu.chinh'. Then you will get id of the record you selected in 'ma_nv' in the onchange function. Then you can browse with this id as you did.

 

 

Avatar
Discard
Best Answer

Hello,

Try this code

class danh_sach_nv(osv.Model):
    _name = 'danh.sach.nv'
    _rec_name = 'ma_nv'
    
    def onchange_luong(self, cr, uid, ids, ma_nv, context=None):
        res = {}
        if ma_nv:
            obj = self.pool.get('dieu.chinh').browse(cr, uid, ma_nv)
            res['luong_ct'] = obj.luong_dc
            
            return {'value': res}
        
    _columns = {
                
               'ma_nv':fields.many2one('dieu.chinh','Mã NV'),

               'luong_ct':fields.char('Lương chính thức',size = 20),

}

 

Avatar
Discard
Author Best Answer

I tried what you told me. but I dont understand why it did not work

Avatar
Discard

What exactly you want do using this oncahnge method, which value you want fetch using this browse code >> self.pool.get('dieu.chinh').browse(cr, uid, ma_nv) ???

Author

yes. I want when in objcet: dieu.chinh change value of fields ma_nv then value in object danh.sach.nv change and equal to value in field in dieu.chinh

I have update ans. .. pls check it

Author

you test my code ?? I checked it , but it did not work

Author

I tried your code . but it did not work as I want

Update answer try it

I tried it, but it did not work. I dont understand


2014-08-12 17:18 GMT+07:00 Jagdish Panchal <jnp1682@mail.odoo.com>:

Update answer try it

--
Jagdish Panchal Sent by Odoo Inc. using Odoo. Access your messages and documents in Odoo

Author

I tried it, but it did not nothing.

Author

Can you help me? I dont understand your mean? Can you clear for me?

Best Answer

Hi,

I think you should correct the following line. May be my assemption is right.

res['luong_ct'] = obj.luong_dc.name 

Note : If obj.luong_dc is filed like many2one then you have to assign its name to the char field 'luong_ct'.

So, your code may be like below.

 def onchange_luong(self, cr, uid, ids, ma_nv, context=None):

    res = {}
    if ma_nv:
        obj = self.pool.get('dieu.chinh').browse(cr, uid, ma_nv)
        res['luong_ct'] = obj.luong_dc.name
        
        return {'value': res}

    return {'value':res}

Avatar
Discard