This question has been flagged
2 Replies
12957 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?

Avatar
Discard

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?

Best Answer

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>
Avatar
Discard
Best Answer

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]
Avatar
Discard
Author

My requirement little different Sven. Can u help me?

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

Author

How can i get it? Please specify the code.

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

@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 ?

Author

@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 Why are using sql queries in OpenERP, use ORM methods instead. Check the answer below.