Odoo Help


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.


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

Pankaj Moolrajani
on 4/12/14, 2:05 AM 2,555 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....


Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 4/14/14, 5:27 AM

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

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

0 follower(s)


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