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.

3

How to use the value of a selection field instead of its key in a python function ?

By
PY
on 12/13/13, 12:47 PM 7,058 views

Hi !

I have a fields.selection, and I want to use the value of this field in a python function.

But, when I pass the field to this function, I get a string, the key...

So, how can I get the value instead ?

Thanks !

EDIT

Hi ! New week, already no answer for this... I wonder if a way to do this really exists...

5

Felipe

--Felipe--
962
| 5 3 8
Bogotá, Colombia
--Felipe--
Informatics engineer Industrial engineer
Felipe
On 12/16/13, 11:10 AM
  • model is the name of the model, example: 'account.invoice'
  • field is the name of the field inside the model, example: 'state'
  • field_val is the value of that field, example: 'draft'

Code:

dict(self.pool.get(model).fields_get(cr, uid, allfields=[field], context=context)[field]['selection'])[field_val]

Example:

dict(self.pool.get('account.invoice').fields_get(cr, uid, allfields=['state'], context=context)['state']['selection'])['draft']

Hum, this is quite unreadable, can you give me an example ?

PY
on 12/16/13, 11:18 AM

Well, thanks, I'll try this :)

PY
on 12/16/13, 11:22 AM

Works fine, thanks again !

PY
on 12/16/13, 12:02 PM

test=dict(self.pool.get('sale.order').fields_get(cr, uid, allfields=['state'], context=context))['state']['selection']

    print "***************order",test                 ///////////////[('draft', u'Devis brouillon'), ('sent', u'Devis envoy\xe9'), ('cancel', u'Annul\xe9e'), ('waiting_date', u'Attente de planification'), ('progress', u'Bon de commande'), ('manual', u'Commande \xe0 facturer'), ('shipping_except', u"Incident d'exp\xe9dition"), ('invoice_except', u'Incident de facturation'), ('done', u'Termin\xe9e')]
medhabib
on 12/16/13, 12:24 PM
4

Bejaoui Souheil

--Bejaoui Souheil--
808
| 2 1 4
Sousse, Tunisia
--Bejaoui Souheil--

Odoo developer,  functional and technical consultant

souheil_bejaoui@hotmail.fr

Bejaoui Souheil
On 4/1/15, 9:54 AM

The V8 way:

use the @api.one decorator

  • current model :value = dict(self.fields_get(allfields=['state'])['state']['selection'])['key']

  • other model : value = dict(self.env['your.model'].fields_get(allfields=['state'])['state']['selection'])['key']

Hope this help somebody..

1
Alessandro Ruffolo
On 10/1/15, 8:18 AM

I use another technique.

First I define the list of pairs code/value

VALUES = [('a', 'A'), ('b','B'), ('c','C')]

I use it into my field definition

f = fields.Selection(VALUES)

and to return value, given the code, with a dict

dict(VALUES)[self.f]

or with a simple loop

for item in VALUES:
if item[0] == self.f:
return item[1]
0
Alessandro Ruffolo
On 10/1/15, 8:18 AM

I use another technique.

First I define the list of pairs code/value

VALUES = [('a', 'A'), ('b','B'), ('c','C')]

I use it into my field definition

f = fields.Selection(VALUES)

and to return value, given the code, with a dict

dict(VALUES)[self.f]

or with a simple loop

for item in VALUES:
if item[0] == self.f:

there must be some error: i can't explain myself this duplicate answer...

Alessandro Ruffolo
on 10/1/15, 9:28 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: 12/13/13, 12:47 PM
Seen: 7058 times
Last updated: 11/26/15, 11:21 AM