Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
2 Respostas
11483 Visualizações

I am trying to pull data from one table to other on the basis of id in table-1. I am having two tables: othertable1 & othertable2. I want to show the value of field1 of othertable1 in field3 of othertable2 if field1==field4. I ahve tried writing this code, but getting Key Error: (id). So please guide me folks.

from osv import osv, fields
import time
import datetime
import pooler

class pka_learning_othertable1(osv.osv):
_name="pka.learning.othertable1"
_description = 'Get Data From Other Table; This is Other Table-1'
_columns={
    'field1':fields.char('Field-1', size=20),
    'field2':fields.char('Field-2', size=20)
}

pka_learning_othertable1()

class pka_learning_othertable2(osv.osv):
_inherit="pka.learning.othertable1"
_name="pka.learning.othertable2"
_description = 'Get Data From Other Table; This is Other Table-2'

def _get_other_table1(self, cr, uid,ids, field_names, args, context=None):
    res = {}
    #other_table1 = self.pool.get('pka.learning.othertable1')
    other_table1 = self.pool.get('pka.learning.othertable1')
    for record in self.browse(cr, uid, ids, context=context):
        other_table2 = other_table1.search(cr, uid, [('id','=',record.id)])
        for rec in other_table1.browse(cr, uid, other_table2, context=None):
            if rec.field1:
                res[id] = rec.field1

    return res


_columns={
    'field3':fields.function(_get_other_table1, string='Field-3'),
    'field4':fields.char('Field-4', size=50)
}

pka_learning_othertable2()
Avatar
Cancelar
Melhor resposta

Hi

Try below code

def _get_other_table1(self, cr, uid,ids, field_names, args, context=None):
    res = {}
    #other_table1 = self.pool.get('pka.learning.othertable1')
    other_table1 = self.pool.get('pka.learning.othertable1')
    for record in self.browse(cr, uid, ids, context=context):
        other_table2 = other_table1.search(cr, uid, [('id','=',record.id)])
        for rec in other_table1.browse(cr, uid, other_table2, context=None):
            if rec.field1:
                res[record.id] = rec.field1

return res

Thanks

Avatar
Cancelar
Autor

Thanks Jack !

Your problem solve then accept answer

Publicações relacionadas Respostas Visualizações Atividade
2
abr. 21
15777
0
ago. 16
3310
1
mar. 15
5152
1
jul. 25
863
4
fev. 25
3363