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 does the ORM Read() method work exactly?

By
Moin
on 2/12/13, 8:05 AM 13,466 views

I assumed that the read method returns a list of dictionaries (records) which are then directly displayed by the list view or form view but that does not seem to be the case. I have this very simple python object defined as follows:

class time_periods(osv.osv):

def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
    res = super(time_periods, self).read(cr, uid, ids, fields=fields, context=context, load='_classic_read')
            #res is empty at this point since the underlying table is empty
            #I want to display completely new data (same fields though) in the list view
    res = [{'name': 'Test', 'id':1}, {'name': 'Test 2', 'id':2}]

    print res
    return res

_name = 'time.periods'

_columns = {
    'name': fields.char('Name', size=50),
        }

time_periods()

currently the corresponding table in the OpenERP database is empty but I assumed that the hardcoded list(res) would act as records retrieved from the database however the list view still renders enpty.

As an experiment, I created a record with id = 2, name = "Test" in the database and the tree view showed one record but with name = "Test 2" as defined in the list(res).

The idea is to replace the list(res) with data retrived from an external database through xmlrpc call.

So, how does the read method work exactly?

Thank You.

EDIT

Just to make my problem clearer, the time_periods table in the OpenERP database is empty so the super call to the read method returns an empty list. What I want to do is display data (from another database but with same fields as the python object) in OpenERP even when the local database is empty. So, essentially I want to intercept the data source of the list view and attach my own data in it not simply make changes to existing data since there is no data.

Try to format your code, I think that you're replaced the content of res, use two variables res1 and res2 and display them with print :)

Yug Faa
on 2/12/13, 12:18 PM

use res.get(NAME_OF_FIELD) to get values from res

Yug Faa
on 2/12/13, 12:19 PM

It doesn't matter that I override the content of res since res is an empty list by default (the table is empty) I want to display new records entirely at run time despite there being no data in the OpenERP database.

Moin
on 2/12/13, 12:57 PM
1
Yohanes Iwan Sugiarto
On 2/12/13, 12:15 PM

Hi Moin!

Here is a simple example to demonstrate the read method. Just for demonstration sake, this code will add triple asterisks '***' in front of the name field of product_category field.

You can replace line 8 with another code to replace the value from another database.

def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
    if context is None:
        context = {}
    res = super(product_category, self).read(cr, uid, ids, fields=fields, context=context, load=load)
    idx = 0
    for r in res:
        if r.has_key('name'):
            r['name'] = '***' + r['name']
            #replace line above with replacement value from external database
        res[idx] = r
        idx = idx + 1
    return res

Thanks for your answer Yohanes, although I think I may have not described my problem correctly. See the EDIT for clarification please. Thank you again for your time.

Moin
on 2/12/13, 12:55 PM
priyankahdp
on 3/28/13, 3:28 PM
0
Noel Jesús Rivero
On 5/29/14, 5:21 PM

Hello , I'm having the same problem. I can't find a way to do this. Do you have a solution?
 

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

2 follower(s)

Stats

Asked: 2/12/13, 8:05 AM
Seen: 13466 times
Last updated: 3/16/15, 8:10 AM