Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
3 Vastaukset
3856 Näkymät

Hi all ! I defined a function. but when I used it , the message error  is : TypeError: _read() takes at least 6 arguments (4 given). 

I dont know i have an error. I need some your help.

This is my code  .py

 

 def _read(self, cr, uid, ids, field_name ,arg, context = None):
        res = {}
        obj = self.pool.get('danh.sach.nv').browse(cr, uid, ma_nv, context = context)
        ma_nvs = self.search(cr, uid, [('obj','=',ma_nv)], context=context)
        res = self.read(cr, uid, ma_nvs , ['ten_bp', 'ten_cd'])
        return { ' value' : res}

    _columns = {
                
                'ma_nv':fields.many2one('danh.sach.nv', 'Mã NV'),
                'ten_nv':fields.char( 'Tên nhân viên', size = 30),
                'ngay_sinh':fields.date('Ngày sinh'),
                'ten_bp':fields.char('Bộ phận', size = 30),
                
                'ten_cd':fields.char( 'Chức danh', size = 30),
               
                'ly_do_huy': fields.char('Lý do hủy hợp đồng', size = 100),
                'ngay_huy':fields.date('Ngày hủy'),
                
                }
    _defaults = {
                 'ten_bp': _read,
                 'ten_cd' : _read
                 }

 

 

I need  your help. 

Thanks all

Avatar
Hylkää
Paras vastaus

Hi,

Just change the name of the function and write down as like below.

def _default_bp(self, cr, uid, context = None):   

    .... your code for bp ....

    return 'any string'

def _default_cd(self, cr, uid, context = None):    

    .... your code for cd ....

    return 'any string'

_columns = {

                'ma_nv':fields.many2one('danh.sach.nv', 'Mã NV'),
                'ten_nv':fields.char( 'Tên nhân viên', size = 30),
                'ngay_sinh':fields.date('Ngày sinh'),
                'ten_bp':fields.char('Bộ phận', size = 30),
                
                'ten_cd':fields.char( 'Chức danh', size = 30),
               
                'ly_do_huy': fields.char('Lý do hủy hợp đồng', size = 100),
                'ngay_huy':fields.date('Ngày hủy'),
                
                }
    _defaults = {
                 'ten_bp': _default_bp,
                 'ten_cd' : _default_cd
                 }

Avatar
Hylkää
Tekijä Paras vastaus

Thanks Chirag. But I tried it. my code .py

    def _get_bp(self, cr, uid, *args):
        try:
            proxy = self.pool.get('danh.sach.nv').browse(cr, uid, ids).ma_nv
            md = self.browse(cr, uid, ids).ma_nv
            if proxy==md :
                res['ten_bp'] = proxy.ten_bp
            return {'value': res}
        except Exception, ex:
            return False 

 

 

But it did not work. I dont know I have error. Can you help me?

Avatar
Hylkää

hi, You have to take following things in mind. 1) In this kind of function you did not get ids. You have to fetch details without ids. 2) You have to return 'string' not Dictionary. (In your case). Can you write down your objective over here ?

Tekijä

hi. Thanks your help. I want the value fields in class danh.sach.sv which will fill to field in class : huy.hd when two records has value to equal of value fields : ma_nv. I want to ask you a question : when I use on_change method which limit number field to load??? because when I creat 3 field then server report a error : overload???I dont understand?

Paras vastaus

You are mixing up function fields, on_change methods and default methods.

What you try to do is setting default values with a function. The syntax for that is the one that chirag pointed out. Use it!

Further it looks like ten_bp should default to a value that has something to do with  danh.sach.nv (ma_nv), but you must not forget, that the default values will be calculated on the creation of a record. Thus there is no related ma_nv assigned to the new object yet. You can not browse a record that has not yet been created. 

Avatar
Hylkää