Order unsorted Selection by index definition
issue11169 review387771003
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
* Order not sorted Selection by index definition
|
||||
* Add optional column on tree view
|
||||
* Use dictionary as domain on Reference field
|
||||
* Include model, field and column in import data error message
|
||||
|
||||
@@ -543,6 +543,11 @@ A string field with limited values to choose from.
|
||||
If true, the choices will be sorted by human-readable value. Default value
|
||||
is ``True``.
|
||||
|
||||
.. note::
|
||||
If it is ``False``, search results ordered by the field will use the
|
||||
index of the selection instead of the human-readable name.
|
||||
..
|
||||
|
||||
.. attribute:: Selection.selection_change_with
|
||||
|
||||
A set of field names. If this attribute is set, the client will call the
|
||||
|
||||
@@ -132,11 +132,16 @@ class Selection(SelectionMixin, Field):
|
||||
else:
|
||||
selections = []
|
||||
column = self.sql_column(table)
|
||||
if not self.sort:
|
||||
else_ = len(selections) + 1
|
||||
selections = ((k, i) for i, (k, v) in enumerate(selections))
|
||||
else:
|
||||
else_ = column
|
||||
whens = []
|
||||
for key, value in selections:
|
||||
whens.append((column == key, value))
|
||||
if whens:
|
||||
return [Case(*whens, else_=column)]
|
||||
return [Case(*whens, else_=else_)]
|
||||
else:
|
||||
return [column]
|
||||
|
||||
|
||||
@@ -19,6 +19,12 @@ class Selection(ModelSQL):
|
||||
'Static Selection')
|
||||
dyn_select_static_string = dyn_select_static.translated(
|
||||
'dyn_select_static')
|
||||
unsorted_select = fields.Selection([
|
||||
(None, ""),
|
||||
('first', "First"),
|
||||
('second', "Second"),
|
||||
('last', "Last"),
|
||||
], "Unsorted Selection", sort=False)
|
||||
|
||||
@fields.depends('select')
|
||||
def get_selection(self):
|
||||
|
||||
@@ -172,6 +172,25 @@ class FieldSelectionTestCase(unittest.TestCase):
|
||||
|
||||
self.assertListEqual(values, ['3', '2', '1', '0x3', '0x2', '0x1'])
|
||||
|
||||
@with_transaction()
|
||||
def test_search_order_unsorted(self):
|
||||
"Test search order with unsorted selection"
|
||||
pool = Pool()
|
||||
Selection = pool.get('test.selection')
|
||||
|
||||
Selection.create([{
|
||||
'unsorted_select': 'second',
|
||||
}, {
|
||||
'unsorted_select': 'last',
|
||||
}, {
|
||||
'unsorted_select': 'first',
|
||||
}])
|
||||
|
||||
records = Selection.search([], order=[('unsorted_select', 'ASC')])
|
||||
values = [r.unsorted_select for r in records]
|
||||
|
||||
self.assertListEqual(values, ['first', 'second', 'last'])
|
||||
|
||||
@with_transaction()
|
||||
def test_string(self):
|
||||
"Test string selection"
|
||||
|
||||
Reference in New Issue
Block a user