This question has been flagged
1 Reply
23963 Views

Hi,

How I can read or return the value chosen from a selection field? I need the user to select values from a list, and use the selected value to complete the information in other fields in the form.

Thanks.

Some code used to test, with error:

....
 'seq_choosed': fields.selection([(11,'Product End'),(12,'Product Base')],'Sequence to use '),
...
 lot_sequence = self.pool.get('ir.sequence').get_id(cr, uid, seq_choosed.value, context={} )
Avatar
Discard
Best Answer

Your question does not clearly explain what you want to do nor what you tried. You say you have an error but you do not say what is the error message (and trace). Your code example is not complete enough to understand properly.

Still, here are a few pointers:

  • How to obtain the value chosen by the user depends on where you do it: in an on_change method, in a fields.function, when overriding one of the base ORM methods, etc.
  • Have a look at the OpenERP Technical memento, it contains examples for these various use cases.
  • The selection field in your example has 2 possible hardcoded integer values: 11 or 12. This is rather unusual but supported. Most selection fields use string values though. In this case when you read() or browse() a record from the model in which this field is defined, you will get the value as an int (11 or 12) or False if the field was left empty (if it's not required).

Here's a random example based on the little information you provided:

Imagine you put an on_change on your field in the XML view:

  <field name="seq_choosed" on_change="on_change_seq_choosed(seq_choosed)"/>
  <field name="seq_num"/>

Then you could do something like the following:

  def on_change_seq_choosed(self, cr, uid, ids, seq_choosed, context=None):
       # on_change returns a dict
       result = {
           'value': {}
       }
       # selected value is explicitly passed in seq_choosed parameter in XML def 
       if seq_choosed:
           seq_num = self.pool.get('ir.sequence').get_id(cr, uid, seq_choosed, context=context)
           result['value']['seq_num'] = seq_num
       return result

That's not a very useful example, but if you want better answers you'll have to be a lot more specific in your question ;-)

Avatar
Discard
Author

Thanks a lot for your answer, and all the corrections, my English is no very good. I going to try this, and looking more about the on_change method.

@Oliver Dony, for instance I want to print the value (Product Base) with key (12), how to do?

If I print seq_choosed, it prints me the key of selection field. I need to print value of key chosen