Add sort and translate options to Reference field
As Reference field uses the SelectionMixin, they could have the same options. issue8278 review253331002
This commit is contained in:
1 parent
3764a8c44e
commit
e39129ee3d
5 files changed
+23
-13
No files matched your search
@@ -1,3 +1,4 @@
|
||||
* Add sort and translate options to Reference field
|
||||
* Do not create empty translations
|
||||
* Replace dsn by params to connect to postgresql
|
||||
* Simplify cron
|
||||
|
||||
@@ -446,7 +446,7 @@ A binary field. It will be represented in Python by a ``bytes`` instance.
|
||||
Selection
|
||||
---------
|
||||
|
||||
.. class:: Selection(selection, string[, sort[, selection_change_with[, translate[, \**options]]])
|
||||
.. class:: Selection(selection, string[, sort[, selection_change_with[, translate[, \**options]]]])
|
||||
|
||||
A string field with limited values to choice.
|
||||
|
||||
@@ -505,7 +505,7 @@ Instance methods:
|
||||
Reference
|
||||
---------
|
||||
|
||||
.. class:: Reference(string[, selection[, selection_change_with[, search_order[, search_context[, \**options]]]])
|
||||
.. class:: Reference(string[, selection[, sort[, selection_change_with[, translated[,search_order[, search_context[, \**options]]]]]]])
|
||||
|
||||
A field that refers to a record of a model. It will be represented in Python by
|
||||
a ``str`` instance like this::
|
||||
@@ -520,10 +520,18 @@ But a ``tuple`` can be used to search or set value.
|
||||
|
||||
Same as :attr:`Selection.selection` but only for model name.
|
||||
|
||||
.. attribute:: Reference.sort
|
||||
|
||||
Same as :attr:`Selection.sort`.
|
||||
|
||||
.. attribute:: Reference.selection_change_with
|
||||
|
||||
Same as :attr:`Selection.selection_change_with`.
|
||||
|
||||
.. attribute:: Reference.translate_selection
|
||||
|
||||
Same as :attr:`Selection.translate_selection`.
|
||||
|
||||
.. attribute:: Reference.datetime_field
|
||||
|
||||
Same as :attr:`Many2One.datetime_field`
|
||||
|
||||
@@ -99,7 +99,7 @@ class UIMenu(DeactivableMixin, sequence_ordered(), tree(separator=' / '),
|
||||
('ir.action.act_window', 'ir.action.act_window'),
|
||||
('ir.action.wizard', 'ir.action.wizard'),
|
||||
('ir.action.url', 'ir.action.url'),
|
||||
]), 'get_action', setter='set_action')
|
||||
], translate=False), 'get_action', setter='set_action')
|
||||
action_keywords = fields.One2Many('ir.action.keyword', 'model',
|
||||
'Action Keywords')
|
||||
favorite = fields.Function(fields.Boolean('Favorite'), 'get_favorite')
|
||||
|
||||
@@ -21,15 +21,17 @@ class Reference(SelectionMixin, Field):
|
||||
_type = 'reference'
|
||||
_sql_type = 'VARCHAR'
|
||||
|
||||
def __init__(self, string='', selection=None, selection_change_with=None,
|
||||
search_order=None, search_context=None, help='', required=False,
|
||||
readonly=False, domain=None, states=None, select=False,
|
||||
on_change=None, on_change_with=None, depends=None, context=None,
|
||||
loading='lazy', datetime_field=None):
|
||||
def __init__(self, string='', selection=None, sort=True,
|
||||
selection_change_with=None, translate=True, search_order=None,
|
||||
search_context=None, help='', required=False, readonly=False,
|
||||
domain=None, states=None, select=False, on_change=None,
|
||||
on_change_with=None, depends=None, context=None, loading='lazy',
|
||||
datetime_field=None):
|
||||
'''
|
||||
:param selection: A list or a function name that returns a list.
|
||||
The list must be a list of tuples. First member is an internal name
|
||||
of model and the second is the user name of model.
|
||||
:param sort: A boolean to sort or not the selections.
|
||||
:param datetime_field: The name of the field that contains the datetime
|
||||
value to read the target records.
|
||||
:param search_order: The order to use when searching for a record
|
||||
@@ -52,6 +54,8 @@ class Reference(SelectionMixin, Field):
|
||||
'use the depends decorator',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
self.selection_change_with |= set(selection_change_with)
|
||||
self.sort = sort
|
||||
self.translate_selection = translate
|
||||
self.__search_order = None
|
||||
self.search_order = search_order
|
||||
self.__search_context = None
|
||||
|
||||
@@ -13,6 +13,7 @@ from ...rpc import RPC
|
||||
|
||||
class SelectionMixin(Field):
|
||||
translate_selection = True
|
||||
sort = True
|
||||
|
||||
def translated(self, name=None):
|
||||
"Return a descriptor for the translated value of the field"
|
||||
@@ -39,6 +40,7 @@ class SelectionMixin(Field):
|
||||
selection = self.selection
|
||||
definition['selection'] = selection
|
||||
definition['selection_change_with'] = list(self.selection_change_with)
|
||||
definition['sort'] = self.sort
|
||||
return definition
|
||||
|
||||
def definition_translations(self, model, language):
|
||||
@@ -117,11 +119,6 @@ class Selection(SelectionMixin, Field):
|
||||
else:
|
||||
return [column]
|
||||
|
||||
def definition(self, model, language):
|
||||
definition = super().definition(model, language)
|
||||
definition['sort'] = self.sort
|
||||
return definition
|
||||
|
||||
|
||||
class TranslatedSelection(object):
|
||||
'A descriptor for translated value of Selection field'
|
||||
|
||||
Reference in new issue
Block a user