Django管理员列排序降序 [英] Django Admin Column Sort Descending

查看:163
本文介绍了Django管理员列排序降序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将Django admin与



添加以下两行:

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

#换行

default_order_type = getattr(attr, admin_order_first_type,无)
new_order_type = default_order_type,如果default_order_type否则为'asc'

#新行结尾

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

现在在ModelAdmin中,您可以:

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

def number_of_players(self,team ):
返回整数(team.number_of_players)

number_of_players.short_description ='#玩家数量'
number_of_players.admin_order_field ='number_of_players'
number_of_players.admin_order_first_type = 'desc'#将使该列按顺序排序,首先

我对其进行了测试并成功


When using Django admin with grappelli I would like that a click on a column header will sort the table by descending order.

(I don't want a default ordering to the columns by defining the ordering field in the Model Meta Class.)

The default behavior is Ascending.

The first click should order like this:

解决方案

Rewrite the def result_headers(cl)

add these 2 lines:

    .....
    .....
    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
    ...
    ...

now in the ModelAdmin you can:

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

I tested it and it works

这篇关于Django管理员列排序降序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆