跳至內容
選單
此問題已被標幟
4 回覆
7611 瀏覽次數

Hi everybody, I want to ask you a question. I have an error :TypeError: string indices must be integers

code .py

class danh_sach_nv(osv.Model):
    _name = 'danh.sach.nv'
    _rec_name = 'ma_nv'
    

            
    _columns = {
                       'ma_nv':fields.char('Mã NV',size=30),

                       'luong_dc':fields.related('ma_nv', 'luong_dc', type='many2one', relation='dieu.chinh', store=True, string='Lương điều chỉnh', readonly = 'True'),

                  }

class dieu_chinh(osv.Model):
    _name = 'dieu.chinh'

 _columns = {
                    'state': fields.selection([('draft', 'Draft'),('cancel', 'Cancel'),('done', 'Confirmed') ],                                       'Status',readonly=True,track_visibility='onchange'),
                   'image_nv':fields.related('ma_nv', 'image_nv',readonly = 'True', type ='binary', string = 'Ảnh đại diện'),
                   'ma_nv':fields.many2one('danh.sach.nv', 'Mã NV'),

                   'luong_dc':fields.integer('Lương điều chỉnh'),

}

 

 

Can you help me?I dont understan I have why I have an error?

Thank all

頭像
捨棄

Hi huongcute you must change this line: 'luong_dc':fields.related('ma_nv', 'luong_dc', type='many2one', relation='dieu.chinh', store=True, string='Lương điều chỉnh', readonly = 'True'), instead of ma_nv you must give the class name(many2one).Thats why the error occured...Thank u

Sorry other class's(dieu_chinh) field name

最佳答案

Here you go!
You have used related field luong_dc from dieu.chinh model which is defined as integer inside, but in danh.sach.nv model as you have use as relational field you have given many2one. but field type must be same. if you are realting each other.

Hope this will helps you.

Anil.

頭像
捨棄
最佳答案

Hi,

There is wrong defination of the fields.related.

 _columns = {
                       'ma_nv':fields.char('Mã NV',size=30),

                       'luong_dc':fields.related('ma_nv', 'luong_dc', type='many2one', relation='dieu.chinh', store=True, string='Lương điều chỉnh', readonly = 'True'),

                  }

In the above columns you have define 'ma_nv' is 'fields.char' and then you have define 'luong_dc' as 'fields.relate' to the relation with 'ma_nv'. This is wrong, you can only relate any field with 'many2one' field. So, you have to take 'ma_nv' of 'may2one' type or you have to remove 'luong_dc' from 'fields.related' and make it 'fields.many2one'.

 

 

頭像
捨棄
最佳答案

Before creating a related fields, one should have many2one field ... only then a related relation can be establised..

For example:

              'partner_id': fields.many2one('res.partner', 'Partner')

               'partner_email': fields.related('partner_id', 'email', ....)

As you can see, I have establised a relation to partner object first, then i can establish related for the same....

and also data type/property for the related field should be of target objects' column's data type...

 

頭像
捨棄
最佳答案

In short you should define related field like this:

 'luong_dc':fields.related('ma_nv', 'luong_dc', type='integer, store=True, string='Lương điều chỉnh', readonly=True),

One more thing, readonly value should not be in string form (readonly='True'). It should be readonly=True.

You can get more idea about Related field.

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
0
10月 23
1295
3
4月 20
3513
1
2月 20
2235
1
3月 15
4275
1
7月 25
507