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

Working with fields.selection values

By
Leopo
on 2/15/13, 4:51 PM 5,769 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={} )
2

Olivier Dony (odo)

--Olivier Dony (odo)--
2112
| 7 6 7
--Olivier Dony (odo)--

 

Olivier Dony (odo)
On 2/15/13, 5:53 PM

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 ;-)

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.

Leopo
on 2/15/13, 6:23 PM

@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

Atchuthan - Technical Consultant, Sodexis Inc
on 12/13/13, 9:21 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

2 follower(s)

Stats

Asked: 2/15/13, 4:51 PM
Seen: 5769 times
Last updated: 3/16/15, 8:10 AM