2

当将 Django admin 与grappelli一起使用时,我希望单击列标题将按降序对表格进行排序。

(我不希望通过在模型元类中定义排序字段来对列进行默认排序。)

默认行为是升序。

一次点击应该是这样的:

要求的行为

4

1 回答 1

1

重写def result_headers(cl)

添加这两行:

    .....
    .....
    th_classes = ['sortable']
    order_type = ''

    #new lines

    default_order_type = getattr(attr, "admin_order_first_type", None)
    new_order_type = default_order_type if default_order_type else 'asc'

    #end of new lines

    sort_priority = 0
    sorted = False
    ...
    ...

现在在 ModelAdmin 中,您可以:

list_display = ('number_of_players', ....)

def number_of_players(self, team):
        return intcomma(team.number_of_players)

number_of_players.short_description = '# num of players'
number_of_players.admin_order_field = 'number_of_players'
number_of_players.admin_order_first_type = 'desc' #will make the column to be ordered desc first

我测试了它并且它有效

于 2012-10-16T14:46:09.767 回答