Odoo Help


getting price of product.

on 5/24/13, 3:00 AM 1,087 views

hi i created two seperate field for product and its price . now i am able to select the product from the list.

i want to add an additional feature that when i select the product in the product field the price of the product will be came under the price field automatically

x_product(manytoone) , x_price(float)

what should i do in the onchange of xml form view.

send me a created code i will updated there...

on 5/24/13, 3:34 AM

<?xml version="1.0"?> <form string="Purchase Order" version="7.0"> <group> <field name="x_product"/> <field name="x_price"/> </group> </form>

on 5/24/13, 3:46 AM

model= x_product, fields= x_product->many to one relation , x_price -float .. with onchange function what should i do.

on 5/24/13, 3:48 AM

which part i update in the form view using onchange function.

on 5/24/13, 3:51 AM

here no need for py file just i do with the xml form view.

on 5/24/13, 3:56 AM

Product name is a char how u create a many2one function... what u want here in product master creating a product, after u selecting a product then price will come automatically correct...

on 5/24/13, 4:08 AM

onchange function is first create in py file then come to .xml file. that is the procedure.

on 5/24/13, 4:10 AM

is there any way to implement onchange function on the xml file without going to py file. , many to one function is used to list the product in the list automatically when we press on the drop down arrow..

on 5/24/13, 4:18 AM

when selecting the product the price will appear on the price field..

on 5/24/13, 4:25 AM

sorry sir . you are right. thanks for your valuable comments..

on 5/24/13, 5:00 AM
On 5/24/13, 4:52 AM

In the sale Order selecing the product it will automatically calling, try this,

def product_id_change(self, cr, uid, ids, pricelist, product, qty=0, uom=False, qty_uos=0, uos=False, name='', partner_id=False, lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None): context = context or {} lang = lang or context.get('lang',False) if not partner_id: raise osv.except_osv(_('No Customer Defined !'), _('Before choosing a product,\n select a customer in the sales form.')) warning = {} product_uom_obj = self.pool.get('product.uom') partner_obj = self.pool.get('res.partner') product_obj = self.pool.get('product.product') context = {'lang': lang, 'partner_id': partner_id} if partner_id: lang = partner_obj.browse(cr, uid, partner_id).lang context_partner = {'lang': lang, 'partner_id': partner_id}

    if not product:
        return {'value': {'th_weight': 0,
            'product_uos_qty': qty}, 'domain': {'product_uom': [],
               'product_uos': []}}
    if not date_order:
        date_order = time.strftime(DEFAULT_SERVER_DATE_FORMAT)

    result = {}
    warning_msgs = ''
    product_obj = product_obj.browse(cr, uid, product, context=context_partner)

    uom2 = False
    if uom:
        uom2 = product_uom_obj.browse(cr, uid, uom)
        if product_obj.uom_id.category_id.id != uom2.category_id.id:
            uom = False
    if uos:
        if product_obj.uos_id:
            uos2 = product_uom_obj.browse(cr, uid, uos)
            if product_obj.uos_id.category_id.id != uos2.category_id.id:
                uos = False
            uos = False
    fpos = fiscal_position and self.pool.get('account.fiscal.position').browse(cr, uid, fiscal_position) or False
    if update_tax: #The quantity only have changed
        result['tax_id'] = self.pool.get('account.fiscal.position').map_tax(cr, uid, fpos, product_obj.taxes_id)

    if not flag:
        result['name'] = self.pool.get('product.product').name_get(cr, uid, [product_obj.id], context=context_partner)[0][1]
        if product_obj.description_sale:
            result['name'] += '\n'+product_obj.description_sale
    domain = {}
    if (not uom) and (not uos):
        result['product_uom'] = product_obj.uom_id.id
        if product_obj.uos_id:
            result['product_uos'] = product_obj.uos_id.id
            result['product_uos_qty'] = qty * product_obj.uos_coeff
            uos_category_id = product_obj.uos_id.category_id.id
            result['product_uos'] = False
            result['product_uos_qty'] = qty
            uos_category_id = False
        result['th_weight'] = qty * product_obj.weight
        domain = {'product_uom':
                    [('category_id', '=', product_obj.uom_id.category_id.id)],
                    [('category_id', '=', uos_category_id)]}
    elif uos and not uom: # only happens if uom is False
        result['product_uom'] = product_obj.uom_id and product_obj.uom_id.id
        result['product_uom_qty'] = qty_uos / product_obj.uos_coeff
        result['th_weight'] = result['product_uom_qty'] * product_obj.weight
    elif uom: # whether uos is set or not
        default_uom = product_obj.uom_id and product_obj.uom_id.id
        q = product_uom_obj._compute_qty(cr, uid, uom, qty, default_uom)
        if product_obj.uos_id:
            result['product_uos'] = product_obj.uos_id.id
            result['product_uos_qty'] = qty * product_obj.uos_coeff
            result['product_uos'] = False
            result['product_uos_qty'] = qty
        result['th_weight'] = q * product_obj.weight        # Round the quantity up

    if not uom2:
        uom2 = product_obj.uom_id
    # get unit price

    if not pricelist:
        warn_msg = _('You have to select a pricelist or a customer in the sales form !\n'
                'Please set one before choosing a product.')
        warning_msgs += _("No Pricelist ! : ") + warn_msg +"\n\n"
        price = self.pool.get('product.pricelist').price_get(cr, uid, [pricelist],
                product, qty or 1.0, partner_id, {
                    'uom': uom or result.get('product_uom'),
                    'date': date_order,
        if price is False:
            warn_msg = _("Cannot find a pricelist line matching this product and quantity.\n"
                    "You have to change either the product, the quantity or the pricelist.")

            warning_msgs += _("No valid pricelist line found ! :") + warn_msg +"\n\n"
            result.update({'price_unit': price})
    if warning_msgs:
        warning = {
                   'title': _('Configuration Error!'),
                   'message' : warning_msgs
    return {'value': result, 'domain': domain, 'warning': warning}


on_change="product_id_change(parent.pricelist_id, product_id, product_uom_qty, product_uom, product_uos_qty, product_uos, name, parent.partner_id, False, True, parent.date_order, False, parent.fiscal_position, False, context)"/>

try this procedure it will be usefull.

Looks like you want to copy the complete on_change method in your own module. Is that true? Or do you want to modify the existing on_change method? Both options are dangerous...

on 5/24/13, 7:55 PM
Andreas Maertens
On 5/24/13, 3:39 AM

write an onchange method in the model and use that one from the xml form view to alter your fields.

here's an example from sale.py:

def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, order_lines, context=None):
        context = context or {}
        if not pricelist_id:
            return {}
        value = {
            'currency_id': self.pool.get('product.pricelist').browse(cr, uid, pricelist_id, context=context).currency_id.id
        if not order_lines:
            return {'value': value}
        warning = {
            'title': _('Pricelist Warning!'),
            'message' : _('If you change the pricelist of this order (and eventually the currency), prices of existing order lines will not be updated.')
        return {'warning': warning, 'value': value}

while installing the created module an error message will be show like this.... .AttributeError: 'module' object has no attribute 'model'

on 5/30/13, 9:56 AM

The Code above is a copy from "sale.py" in "stock"-module. I guess the failure is somewhere else in your addon. I probably could help you more if you post your source code from reasonable files. ;)

Andreas Maertens
on 5/30/13, 10:44 AM

from openerp.osv import fields,osv

class price(osv.Model): _name = "x_product" _description ="productlist" _coloums = {

on 5/30/13, 10:53 AM

'x_product': fields.many2one('Product.product','product'), 'x_price':fields.float('price',required="false"), } price() def onchange_pricelist_id(self, cr, uid, ids, pricelist_id, order_lines, context=None):

on 5/30/13, 10:53 AM

context = context or {} if not pricelist_id: return {} value = { 'currency_id': self.pool.get('product.pricelist').browse(cr, uid, pricelist_id, context=context).currency_id.id } if not order_lines: return {'value': value} warning = {

on 5/30/13, 10:53 AM

'title': _('Pricelist Warning!'), 'message' : _('If you change the pricelist of this order (and eventually the currency), prices of existing order lines will not be updated.') } return {'warning': warning, 'value': value}

on 5/30/13, 10:54 AM

here it is: class price(osv.Model): it should be: class price(osv.osv):

Andreas Maertens
on 5/30/13, 12:23 PM

sir i try with your suggestion in the eclipseIDE but the error shown in console is ...ImportError: No module named openerp.osv.

on 5/31/13, 2:12 AM

another error displayed while trying to install the module....File "C:\Program Files\OpenERP 7.0-20130509-231040\Server\server\openerp\addons\base\ir\ir_ui_view.py", line 101, in create KeyError: 'arch'

on 5/31/13, 3:31 AM

Please try: from osv import osv, fields . If it's not working you might not have added your server as lib correctly? The osv comes from server/openerp/osv. Maybe just Eclipse cannot solve the import but openerp can do. Your KeyError comes from a view. To help with that please add your view to let me take a view at.

Andreas Maertens
on 5/31/13, 3:52 AM

<?xml version ="1.0" encoding="utf-8"?> <openerp> <data> <record id="customer_order_form" model="ir.ui.view"> <field name="x_product">x_product.form</field> <field name="x_price"/> <field name="arch" type="xml"/> </record> </data> </openerp>

on 5/31/13, 4:58 AM

i tried ..from osv import osv, fields .. but the same error occurs.. so i just created an another one while try to installing that the error shown is like ..Syntax Error: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128).. is it a problem of my compiler. any solutions please.

on 5/31/13, 5:02 AM

Try #-*- coding: utf-8 -*- in the first row of your .py file

Andreas Maertens
on 5/31/13, 5:14 AM

ok that problem is solved the module is to be installed . But the form is not displayed. parent menu and submenu are displayed.

on 5/31/13, 8:07 AM

Did you installed a ir.action_window entry? Checked user permissions?

Andreas Maertens
on 5/31/13, 8:35 AM

sir this i my form view..<?xml version="1.0" encoding="utf-8"?>



   <record model="ir.ui.view" id="test_base_form">

       <field name="name">test.base.form</field>

           <field name="model">test.base</field>

           <field name="type">form</field>

           <field name="arch" type="xml">

               <form string="Test Base">

                   <field name="name"/>

                   <field name="code"/>

on 5/31/13, 9:35 AM



    <record model="ir.ui.view" id="test_base_tree">

        <field name="name">test.base.tree</field>

        <field name="model">test.base</field>

        <field name="type">tree</field>

        <field name="arch" type="xml">

            <tree string="Test Base">

                <field name="name"/>

                <field name="code"/>


on 5/31/13, 9:36 AM


    <record model="ir.actions.act_window" id="action_test_seq">

        <field name="name">Test Base</field>

        <field name="res_model">test.base</field>

        <field name="view_type">form</field>

        <field name="view_mode"> tree,form</field>


    <menuitem id="menu_test_base_main" name="Test Base"/>

    <menuitem id="menu_test_base" parent="menu_test_base_main" name="Test Base" action="action_test_seq"/>



on 5/31/13, 9:36 AM

will you please mention is there any error int the form view.

on 5/31/13, 9:37 AM

So far I cannot see any wrong code in your view. Seems legit to me. Only things I would recomment is to delete the <field name="type"> Tag from your view. This is deprecated. You mention the type in <tree>, <form> or <search> Tag. 2nd thing: add "version="7.0" attribute to your view tag e.g. <form string="foo" version="7.0">. This may not solve the problem but is better code.

Andreas Maertens
on 5/31/13, 9:49 AM

thanks for your valuable suggestions. i delete the things that you mention. i specify the action for the parent menu through the UI. then when click on the menu some error msg shown like this.. Uncaught TypeError: object is not a function http://localhost:8069/web/webclient/js?db=demo_c:3225. when pressing the ok button . the page will appear with the search bar and form view buttton on the right side. Edit ,create button on the left and 'more' button on the middle other fields are empty. when click on edit save and discard button is appeared,no other fields will be present. .

on 5/31/13, 10:21 AM

I Could help better if you could put your code in a separate answer on your question. On bottom you find a button "Answer". Please insert your code here. I'll need your modelfile and the viewfile. If that's provided maybe I can help you on that. Please take care to make it readable (preformated text).

Andreas Maertens
on 6/3/13, 4:09 AM

thanks for your contribution sir .. i just now found a solutions for the problem. "i add the menu action for the parent menu in the xml view" now the view problem is ok. then only one problem in the .py file there display an error that no module found..

on 6/3/13, 4:17 AM

shall i put the .py file.

on 6/3/13, 4:18 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: 5/24/13, 3:00 AM
Seen: 1087 times
Last updated: 3/16/15, 8:10 AM