This question has been flagged
4 Replies
6297 Views

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

Avatar
Discard

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

Best Answer

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.

Avatar
Discard
Best Answer

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'.

 

 

Avatar
Discard
Best Answer

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...

 

Avatar
Discard
Best Answer

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.

Avatar
Discard