Odoo Help

Welcome!

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.

0

Python code for value of a selected option in many2one list

By
rosey
on 9/2/13, 7:46 AM 3,806 views

I'm new in python code.

'source': fields.many2one('sources.sources', 'Source', required=True),

When i select a option in this field, i want to save the id of the value in below field.

'source_value' :

What is the python code for getting the id?

Here my question is, do you want to save it to the database when you select the option or you just want it to display when an option is selected?

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

Gopakumar N G

--Gopakumar N G--
1198
| 5 3 7
Cochin, India
--Gopakumar N G--

Senior Software Engineer at BroadTech IT Solutions Pvt. Ltd.

Gopakumar N G
On 9/4/13, 4:44 AM

If you are displaying both fields in the xml view, you can define an on_change function in the field source_value so that on selecting a source its id can be taken from the on_change function.

The model is

class test_test(osv.osv):
_name="test.test"
_description="test"
_columns={
            'source': fields.many2one('sources.sources', 'Source', required=True),
            'source_value' : fields.integer('Source Value'),
          }
def onchange_source(self, cr, uid, ids, source):
    v={}
    if source:
        source_obj=self.pool.get('sources.sources').browse(cr,uid,source)
        v['source_value']= source_obj.id
    return {'value': v}
test_test()

and xml view is

<record id="view_test_form" model="ir.ui.view">
            <field name="name">test.test.form</field>
            <field name="model">test.test</field>
            <field name="arch" type="xml">
                <form string="Test" version="7.0">
                    <sheet>
                        <group colspan="4" col="4">
                            <field name="source" on_change="onchange_source(source)"/>
                            <field name="source_value"/>
                        </group>
                    </sheet>
                </form>
            </field>
0
Xsias
On 9/2/13, 11:10 AM

The id is save in 'source' field. You want to save the id twice ?

EDIT

@Gopakumar N G. I want id source_value for writing select query in python code. ie, Select cost from cost_table where source=source_value. How can i write select query with this source_value? Please explain.

You can do that with something like that :

my_object = self.browse(cr, uid, object_id)
cost_obj = self.pool.get('cost.table')
cost_ids = cost_obj.search(cr,uid,[('source','=',my_object.source.id)])[0]

My requirement little different Sven. Can u help me?

rosey
on 9/3/13, 2:08 AM

Try to take the id from database if sources.sources contains unique names for each record.

Gopakumar N G
on 9/3/13, 9:55 AM

How can i get it? Please specify the code.

rosey
on 9/3/13, 10:20 AM

@rosey Do you want to display the field source_value in xml view?

Gopakumar N G
on 9/3/13, 11:10 AM

@rosey I can't help you more for the moment because i don't have a good understanding of your need.

Can you explain your requirements ?

Xsias
on 9/3/13, 12:07 PM

@Gopakumar N G. I want id source_value for writing select query in python code. ie, Select cost from cost_table where source=source_value. How can i write select query with this source_value? Please explain.

rosey
on 9/4/13, 2:52 AM

@rosey Why are using sql queries in OpenERP, use ORM methods instead. Check the answer below.

Gopakumar N G
on 9/4/13, 6:43 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)

Stats

Asked: 9/2/13, 7:46 AM
Seen: 3806 times
Last updated: 7/8/15, 4:40 AM