Skip to Content
Menu
This question has been flagged
1 Reply
6740 Views

I use odoo v11. I have a doubt about the selection field. When I create a selection field it appears with a "default value" (represented with a blank space) with the value of "False". I want to hide this False value of the select fields. The reason of this is that I want that the users can only select my options of my select field, because I have a computed field that depends of a selection field. My selection field is this:

estatura_um = fields.Selection([
('m', 'm.'),
('cm', 'Cm.'),
('pies', 'Pies'),
], string='Unidad de medida', default="m")

I search in fields.py the class selection field:

class Selection(Field):
"""
:param selection: specifies the possible values for this field.
It is given as either a list of pairs (``value``, ``string``), or a
model method, or a method name.
:param selection_add: provides an extension of the selection in the case
of an overridden field. It is a list of pairs (``value``, ``string``).

The attribute ``selection`` is mandatory except in the case of
:ref:`related fields <field-related>` or :ref:`field extensions
<field-incremental-definition>`.
"""
type = 'selection'
_slots = {
'selection': None, # [(value, string), ...], function or method name
}

def __init__(self, selection=Default, string=Default, **kwargs):
super(Selection, self).__init__(selection=selection, string=string, **kwargs)

@property
def column_type(self):
if (self.selection and
isinstance(self.selection, list) and
isinstance(self.selection[0][0], int)):
return ('int4', 'integer')
else:
return ('varchar', pg_varchar())

def _setup_regular_base(self, model):
super(Selection, self)._setup_regular_base(model)
assert self.selection is not None, "Field %s without selection" % self

def _setup_related_full(self, model):
super(Selection, self)._setup_related_full(model)
# selection must be computed on related field
field = self.related_field
self.selection = lambda model: field._description_selection(model.env)

def _setup_attrs(self, model, name):
super(Selection, self)._setup_attrs(model, name)
# determine selection (applying 'selection_add' extensions)
for field in reversed(resolve_mro(model, name, self._can_setup_from)):
# We cannot use field.selection or field.selection_add here
# because those attributes are overridden by ``_setup_attrs``.
if 'selection' in field.args:
self.selection = field.args['selection']
if 'selection_add' in field.args:
# use an OrderedDict to update existing values
selection_add = field.args['selection_add']
self.selection = list(OrderedDict(self.selection + selection_add).items())

def _description_selection(self, env):
""" return the selection list (pairs (value, label)); labels are
translated according to context language
"""
selection = self.selection
if isinstance(selection, pycompat.string_types):
return getattr(env[self.model_name], selection)()
if callable(selection):
return selection(env[self.model_name])

# translate selection labels
if env.lang:
name = "%s,%s" % (self.model_name, self.name)
translate = partial(
env['ir.translation']._get_source, name, 'selection', env.lang)
return [(value, translate(label) if label else label) for value, label in selection]
else:
return selection

def get_values(self, env):
""" return a list of the possible values """
selection = self.selection
if isinstance(selection, pycompat.string_types):
selection = getattr(env[self.model_name], selection)()
elif callable(selection):
selection = selection(env[self.model_name])
return [value for value, _ in selection]

def convert_to_cache(self, value, record, validate=True):
if not validate:
return value or False
if value in self.get_values(record.env):
return value
elif not value:
return False
raise ValueError("Wrong value for %s: %r" % (self, value))

def convert_to_export(self, value, record):
if not isinstance(self.selection, list):
# FIXME: this reproduces an existing buggy behavior!
return value if value else ''
for item in self._description_selection(record.env):
if item[0] == value:
return item[1]
return False
Avatar
Discard
Best Answer

estatura_um = fields.Selection([
('m', 'm.'),
('cm', 'Cm.'),
('pies', 'Pies'),
], string='Unidad de medida', default="m", required=True)

Avatar
Discard
Related Posts Replies Views Activity
0
Oct 19
1515
2
Dec 17
14885
0
Apr 15
2726
1
Mar 15
8396
1
Dec 22
3182