3

How can i get selection fields value in odoo 10?

def compute_default_value(self):
    return self.get_value("field")

I tried this,

def compute_default_value(self):
   return dict(self._fields['field'].selection).get(self.type)

Also tried this,but it is not working. Please help me, i could not find the solution.

Thank you.

Naglis
  • 2,583
  • 1
  • 19
  • 24
Serdar Eren
  • 165
  • 2
  • 16
  • Could you provide more information or code? I don't understand this question at all. – CZoellner Sep 20 '17 at 15:05
  • month = fields.Selection([(1, 'Ocak'), (2, 'Şubat'), (3, 'Mart'), (4, 'Nisan'), (5, 'Mayıs'), (6, 'Haziran'), (7, 'Temmuz'), (8, 'Ağustos'), (9, 'Eylül'), (10, 'Ekim'), (11, 'Kasım'), (12, 'Aralık') ], string='Ay',default=datetime.now().month,required="True") This is my selection field, i want to get this selection fields value for use to another field report_date = fields.Char(compute='compute_selection_value'). But i can not get the value of selection field. – Serdar Eren Sep 21 '17 at 06:16

2 Answers2

4

You can do this in a following manner:

self._fields['your_field']._desription_selection(self.env)

This will return the selection list of pairs (value, label).

If you just need possible values, you can use get_values method.

self._fields['your_field'].get_values(self.env)

But it's not a common way. Most of the time people define selections differently and then use those definitions. For example, I commonly use classes for those.

class BaseSelectionType(object):
    """ Base abstract class """

    values = None

    @classmethod
    def get_selection(cls):
        return [(x, cls.values[x]) for x in sorted(cls.values)]

    @classmethod
    def get_value(cls, _id):
        return cls.values.get(_id, False)


class StateType(BaseSelectionType):
    """ Your selection """
    NEW = 1
    IN_PROGRESS = 2
    FINISHED = 3

    values = {
        NEW: 'New',
        IN_PROGRESS: 'In Progress',
        FINISHED: 'Finished'
    }

You can use this class wherever you want, just import it.

state = fields.Selection(StateType.get_selection(), 'State')

And it's really handy to use those in the code. For example, if you want to do something on a specific state:

if self.state == StateType.NEW:
    # do your code ...
tidylobster
  • 683
  • 5
  • 13
0

I don't get the question fully, but let me try to answer. Why not just define the selection as method and use it for both situations:

from datetime import datetime
from odoo import models, fields


class MyModel(models.Model):
    _name = 'my.model'

    def month_selection(self):
        return [(1, 'Month1'), (2, 'Month2')]

    def compute_default_value(self):
        selection = self.month_selection()
        # do whatever you want here

    month = fields.Selection(
        selection=month_selection, string='Month',
        default=datetime.now().month, required=True)
CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • Thank you for your answer but this is not what i want. When i select 'Month1', how can i send this selection to another field. For example show_month = fields.Char() and i want to send 'Month1' to show_month. Is there any method like month.value or month.selection something like that for getting selected item? – Serdar Eren Sep 21 '17 at 07:42