Odoo Help


how to use fields.function to get value from database ?

Pankaj Moolrajani
on 4/12/14, 2:05 AM 3,910 views

Hi Folks,

I have just started understanding openerp. I am customizing it's warehouse & sales modules according to my needs. But i am stuck now. Below is the description of problem, for which i need help:

I want to fetch product location from product_template relation. I will show the fetched product location in tree view of product_product, for which i need to define fields.function(_get_product_loc_case, string='Product Location) function, plz help.

class product_product(osv.osv):

def _get_product_loc_case(self, cr, uid, ids, name, value, args, context=None):
    res = {}
    for product in self.browse(cr, uid, ids):
        res = {'product_loc': product_template.loc_case}

'product_loc': fields.function(_get_product_loc_case, string='Case')

class product_template(osv.osv): 'loc_case': fields.char('Location Case', size=50)

Any help would be appreciated, and sorry if the code is confusing you, coz i really don't know about complex programming in openerp, just trying to learn that....


Hello Pankaj,

You can get the location of product using following code:

def _get_product_loc_case(self, cr, uid, ids, name, args, context=None):
    res = {}
    for product in self.browse(cr, uid, ids, context=context):
        result[product.id] = product.product_tmpl_id and product.product_tmpl_id.loc_case or ''
    return res

class product_product(osv.osv)
    _inherit = 'product.product'
    _column = {
        'product_loc': fields.function(_get_product_loc_case, type='char', string='Case')

Now you just need to inherit the Tree View of product.product and put the our new field product_loc in the Tree View.

Note: You must write the function field method before the field is declare in the file.

Hope this will helps you.

Thank you.

thanks a lot, it solved my problem...:)

Pankaj Moolrajani
on 4/14/14, 6:10 AM

result[product.id] = product.product_tmpl_id and product.product_tmpl_id.loc_case or '', plz explain meainig of this.

Pankaj Moolrajani
on 4/14/14, 6:25 AM

The meaning of the above code is that each record is distinguished by its id right? So we are retrieving the result for a particular record into a field which is a functional field, here the result is product.product_tmpl_id.loc_case and the record distinguisher is its id so we are assigning the result to that record and display the result using that functional field 'product_loc'.

Abhishek H Menon
on 4/16/14, 5:25 AM

About This Community

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 4/12/14, 2:05 AM
Seen: 3910 times
Last updated: 8/5/15, 1:39 PM