Odoo Help


Python code get the value in a integer field by comparing two many2one fields ?

on 8/30/13, 9:42 AM 8,470 views

I have three fields in a table- name1, name2, cost. I already created a form for saving these fields,

And i have another form(another table)---here name1,name2 are many2one fields. When i select these two names Compare the two many2one field, its corresponding value of cost selects from the that table to this form.

How can i write a code for this?

form1 or table1

class cost()

'name1' : fields.many2one('name1.name1', required= true)

'name2' ; fields.many2one('name2.name2', required=true)

'cost' : fields.integer('Name',required=true)


eg: ram sam 1500 john sabu 43

form2 or table2

class select()

'name1' : fields.many2one('name1.name1', required= true)

'name2' ; fields.many2one('name2.name2', required=true)

'select_cost' : compare name1 and name2 select the cost from the first table1

ie, if i select ram,sam then the cost is 1500 in select_cost field or if john or sabu then cost is 43.

how can i write the code in the select_cost field?

How to use select query here?

You have to use on_change() in the view definition of the secong program, and should give the many2one field ids as arguements. And with that arguements, you have to retrieve the cost from the first model. self.pool.get('first_model_name').search(cr,uid,[('name1','=',name1)('name2','='name2)],context=None) after running this command you will get the respective record ids and pass that id to browse that record. you will get browse_record of the first model. then you can retrieve the cost from the first table using . operator to that browse record. and return {'value':value['select_cost']=cost}

Abhishek H Menon
on 9/3/13, 5:22 AM

Hi Abhishek, I'm new in python code. Can u please brief the code? Awaiting your reply.

on 9/3/13, 5:36 AM

Please update the code it as answer.

on 9/3/13, 5:37 AM

Please use the above field name, that i can easily recognize the code.

on 9/3/13, 5:38 AM

class first_mod(osv.osv):

Abhishek H Menon
on 9/3/13, 7:48 AM

Hi Abhishek, please complete the code. Awaiting your reply. Its urgent for me,please

on 9/3/13, 8:11 AM
Abhishek H Menon
On 9/3/13, 8:15 AM
    from osv import fields,osv
    class name(osv.osv):
    class first_model(osv.osv):

And in wizard folder,

from osv import fields,osv

    class second_model(osv.osv_memory):
                  'select_cost':fields.integer('Select Cost')}
        def onchange_cost(self,cr,uid,ids,name1,name2,context=None):
            if name1 and name2:
                if name1 != name2:

            return {'value':value}

And for wizard view,

<record id="view_select_cost" model="ir.ui.view">
              <field name="name">second.model</field>
              <field name="model">second.model</field>

              <field name="arch" type="xml">
                <form string="Select Cost" version="7.0">

                        <field name="name1"/>
                        <field name="name2"  on_change="onchange_cost(name1,name2)"/>
                        <field name="select_cost"/>

        <record id="action_select_cost" model="ir.actions.act_window">
            <field name="name">Select Cost</field>

            <field name="res_model">second.model</field>

            <field name="view_type">form</field>
            <field name="target">new</field>
            <field name="view_mode">form</field>


After all these process, you have to necessary action and menus inorder to display the results properly. Try this

And in your xml view definition for the model second.model in your field , <field name="name2" on_change="onchange_cost('name1','name2')"/> and see the changes

Abhishek H Menon
on 9/3/13, 8:17 AM

Very Thankful to you, lets try & will reply you.

on 9/3/13, 8:32 AM

Hi, But cost is not selecting here. what will be the error?

on 9/3/13, 9:35 AM

put a print statement like print "...", record.cost below that code record=self.pool.get.... and update the module and look in your terminal console. what you are getting?

Abhishek H Menon
on 9/3/13, 9:39 AM

if you select 2 names from those many2one field, the corresponding cost will appear in the second model

Abhishek H Menon
on 9/3/13, 9:41 AM

error is ::: record_id=self.pool.get('first.model').search(cr,uid,[('name1','=',name1),('name1','=',name2)],context=context) . AttributeError: 'NoneType' object has no attribute 'search'

on 9/3/13, 10:13 AM

Madam, you please do one thing. Make that second model as a wizard. then you will easily get that answer. Let me update the answer

Abhishek H Menon
on 9/3/13, 10:53 AM

Now please go for the answer, dont forget to remove the unnecessary print statements.I have given only for test purpose. Thanks

Abhishek H Menon
on 9/3/13, 11:00 AM

Thank you very much Abhishek. Solved.

on 9/4/13, 3:26 AM

If you know implement it as a postgresql query ie select query, it will be more helpful to me, to implement in other places.

on 9/4/13, 3:28 AM

please implement the above code with postgresql query ie,select query. it will be more helpful to me to implement in other places.

on 9/4/13, 3:31 AM

Let me try Miss Rosey

Abhishek H Menon
on 9/6/13, 3:45 AM

create table name_model (pk_int_name_id int primary key,name varchar(20)); this is for creating name_model

Abhishek H Menon
on 9/6/13, 4:06 AM

Then? What is the next step? Same as above?

on 9/6/13, 5:17 AM

yeah do all the tables mentioned above in the same way

Abhishek H Menon
on 9/6/13, 6:43 AM

And don't forget to refer postgreSQL study materials, the methods are same. Thanks

Abhishek H Menon
on 9/6/13, 8:22 AM

i just try this onchange function Select the first field , then select the second field call onchange function will result this warning "Access Denied

Private methods (such as _onchange_fare) cannot be called remotely."

on 9/19/13, 7:01 AM

change the function name from " _onchange_fare " to " onchange_fare"

Abhishek H Menon
on 9/19/13, 7:03 AM

works fine... Abishek

on 9/20/13, 2:38 AM

AJ Schrafel Paper Corp

--AJ Schrafel Paper Corp--
| 4 5 8
Bronx, United States
--AJ Schrafel Paper Corp--

Exploring OpenERP for business

AJ Schrafel Paper Corp
On 8/30/13, 3:57 PM

in your second module you need to put code like

    _columns = {
    '2nd_mod_field': fields.many2one('first.module', '1st_mod_field','Field Name'), 

and then you can access the values in 2nd_mod_field

i cant get your answer, Patently. Please explain.

on 8/31/13, 11:13 AM

Hi Rosey, In OpenERP with the many2one field, the name (view on field many2one on form) will get from field "name" is default. If you want to get another field, you can use _rec_name = "field_name_view_on_many2one". You can refer at http://help.openerp.com/question/29746/problem-in-my-module-with-field-many2one/

OpenERP Vietnam
on 8/31/13, 3:56 PM

HI OpenERP Vietnam, Please read the question. The requirement is differ from your answer. Please reply

on 9/3/13, 3:12 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

1 follower(s)


Asked: 8/30/13, 9:42 AM
Seen: 8470 times
Last updated: 3/16/15, 8:10 AM