Skip to Content
Menu
This question has been flagged
2 Replies
41003 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.

Avatar
Discard

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 :)

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

Author

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.

Best Answer

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
Avatar
Discard
Author

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.

Best Answer

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

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 17
5308
0
Feb 24
1594
3
Oct 24
8413
2
Apr 25
3150
2
Mar 24
5771