简体   繁体   中英

Flask-Admin: sort column by different field

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-[az] 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')

I use brackets like this, however I'm having trouble finding this in the documentation...

column_sortable_list =  [
    'word',
    'created'
    ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM