Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
7 Antwoorden
46970 Weergaven

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...

Avatar
Annuleer
Beste antwoord
from odoo import api, models, _


def get_selection_label(self, object, field_name, field_value):
  return _(dict(self.env[object].fields_get(allfields=[field_name])[field_name]['selection'])[field_value])


class ResPartner(models.Model):
  _inherit = 'res.partner'

    def my_method(self):
        state_value_translated = get_selection_label(self, 'res.partner', 'state', self.state)
Avatar
Annuleer

Thank you! This has helped me to retrieve the selection field's label in Controller side as well.

Beste antwoord
  • 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']
Avatar
Annuleer
Auteur

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

Auteur

Well, thanks, I'll try this :)

Auteur

Works fine, thanks again !

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')]
Beste antwoord

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]
Avatar
Annuleer

Super way ... especially for custom models

Beste antwoord

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..

Avatar
Annuleer
Beste antwoord

you can use this.

dict(self._fields['your_selection_field'].selection).get(self.your_selection_field)

Avatar
Annuleer

This gives me the Lable of the selection field. How to I get the value for the selected option

Beste antwoord

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:

Avatar
Annuleer

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

Gerelateerde posts Antwoorden Weergaven Activiteit
9
apr. 22
28504
1
mrt. 15
3501
0
mei 20
5953
2
dec. 17
16242
1
jun. 16
10946