Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to use browse() to show values from different table ?

By
Pankaj Moolrajani
on 4/17/14, 9:25 AM 2,465 views

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()
2

Jagdish Panchal

--Jagdish Panchal--
2674
| 5 3 6
Gandhinagar, India
--Jagdish Panchal--

OpenERP Developer

Jagdish Panchal
On 4/17/14, 9:52 AM

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

Thanks Jack !

Pankaj Moolrajani
on 4/17/14, 10:04 AM

Your problem solve then accept answer

Jagdish Panchal
on 4/17/14, 11:13 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/17/14, 9:25 AM
Seen: 2465 times
Last updated: 3/16/15, 8:10 AM