Django Admin設(shè)置應(yīng)用程序及模型順序方法詳解
Django默認(rèn)情況下,按字母順序?qū)δP瓦M(jìn)行排序。因此,Event應(yīng)用模型的順序?yàn)镋pic、EventHero、EventVillain、Event
假設(shè)你希望順序是
EventHero、EventVillain、Epic、Event。
用于呈現(xiàn)后臺(tái)indxe頁(yè)面的模板為admin/index.html,對(duì)應(yīng)的視圖函數(shù)為 ModelAdmin.index。
def index(self, request, extra_context=None):
"""
Display the main admin index page, which lists all of the installed
apps that have been registered in this site.
"""
app_list = self.get_app_list(request)
context = {
**self.each_context(request),
'title': self.index_title,
'app_list': app_list,
**(extra_context or {}),
}
request.current_app = self.name
return TemplateResponse(request, self.index_template or
'admin/index.html', context)
默認(rèn)的get_app_list方法用于設(shè)置模型的順序。
def get_app_list(self, request):
"""
Return a sorted list of all the installed apps that have been
registered in this site.
"""
app_dict = self._build_app_dict(request)
# Sort the apps alphabetically.
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
# Sort the models alphabetically within each app.
for app in app_list:
app['models'].sort(key=lambda x: x['name'])
return app_list
因此,可以通過(guò)覆蓋get_app_list方法來(lái)修改顯示順序:
class EventAdminSite(AdminSite):
def get_app_list(self, request):
"""
Return a sorted list of all the installed apps that have been
registered in this site.
"""
ordering = {
"Event heros": 1,
"Event villains": 2,
"Epics": 3,
"Events": 4
}
app_dict = self._build_app_dict(request)
# a.sort(key=lambda x: b.index(x[0]))
# Sort the apps alphabetically.
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
# Sort the models alphabetically within each app.
for app in app_list:
app['models'].sort(key=lambda x: ordering[x['name']])
return app_list
以上代碼app['models'].sort(key=lambda x: ordering[x['name']])用來(lái)設(shè)置默認(rèn)順序。修改后效果如下。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Django Admin中增加導(dǎo)出CSV功能過(guò)程解析
- Django admin組件的使用
- 給Django Admin添加驗(yàn)證碼和多次登錄嘗試限制的實(shí)現(xiàn)
- Django --Xadmin 判斷登錄者身份實(shí)例
- 在django admin中配置搜索域是一個(gè)外鍵時(shí)的處理方法
- django admin管理工具自定義時(shí)間區(qū)間篩選器DateRangeFilter介紹
- Django admin管理工具TabularInline類(lèi)用法詳解
- Django Xadmin多對(duì)多字段過(guò)濾實(shí)例
- 解決Django部署設(shè)置Debug=False時(shí)xadmin后臺(tái)管理系統(tǒng)樣式丟失
- Django-xadmin+rule對(duì)象級(jí)權(quán)限的實(shí)現(xiàn)方式
- 如何使用Django Admin管理后臺(tái)導(dǎo)入CSV
相關(guān)文章
Python3內(nèi)置模塊random隨機(jī)方法小結(jié)
這篇文章主要介紹了Python3內(nèi)置模塊random隨機(jī)方法小結(jié),random是Python中與隨機(jī)數(shù)相關(guān)的模塊,其本質(zhì)就是一個(gè)偽隨機(jī)數(shù)生成器,我們可以利用random模塊基礎(chǔ)生成各種不同的隨機(jī)數(shù),以及一些基于隨機(jī)數(shù)的操作,需要的朋友可以參考下2019-07-07
教你利用python的matplotlib(pyplot)繪制折線(xiàn)圖和柱狀圖
Python繪圖需要下載安裝matplotlib模塊,它是一個(gè)數(shù)學(xué)繪圖庫(kù),我們將使用它來(lái)制作簡(jiǎn)單的圖表,如折線(xiàn)圖和散點(diǎn)圖,下面這篇文章主要給大家介紹了關(guān)于利用python的matplotlib(pyplot)繪制折線(xiàn)圖和柱狀圖的相關(guān)資料,需要的朋友可以參考下2022-05-05
利用python實(shí)現(xiàn)短信和電話(huà)提醒功能的例子
今天小編就為大家分享一篇利用python實(shí)現(xiàn)短信和電話(huà)提醒功能的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Pytorch 統(tǒng)計(jì)模型參數(shù)量的操作 param.numel()
這篇文章主要介紹了Pytorch 統(tǒng)計(jì)模型參數(shù)量的操作 param.numel(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
樹(shù)莓派3 搭建 django 服務(wù)器的實(shí)例
今天小編就為大家分享一篇樹(shù)莓派3 搭建 django 服務(wù)器的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python3多進(jìn)程 multiprocessing 模塊實(shí)例詳解
這篇文章主要介紹了Python3多進(jìn)程 multiprocessing 模塊,結(jié)合實(shí)例形式詳細(xì)分析了Python3多進(jìn)程 multiprocessing 模塊的概念、原理、相關(guān)方法使用技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06

