I'm trying to control the sorting of a model list in Flask-Admin, and I'm a little confused about how column_sortable_list
works. (I believe that's what I should be using.)
In my case, I have two columns of interest: "word", a text column, and "created", a timestamp. The catch is, I want "word" to be sorted on a separate database field (in the same table), "sortable_word", which is a version of "word" regularized to lowercase, with accented characters replaced by their non-accented versions, all non-[a-z] characters stripped out, etc.
The docs say "If you want to explicitly specify field/column to be used while sorting, you can use a tuple", and shows the example column_sortable_list = ('name', ('user', 'user.username'))
, but either the example isn't right, or I don't understand the format.
For me, column_sortable_list = ('word', ('word', 'sortable_word'))
doesn't work; "word" is sorted based on "word" only. Using ('word', ('word', 'sortable_word'), 'created')
fails in the same way, though also sorts by "created" (correctly). If I use (('word', 'sortable_word'), 'created')
, then only "created" is sortable.
What is the correct syntax for this?
Write your columns as follows:
# Only sorting on one column
column_sortable_list = (('word', ('sortable_word', )), )
# multiple sort columns
column_sortable_list = (('word', ('sortable_word', )), 'created')