Django Admin設(shè)置應(yīng)用程序及模型順序方法詳解
Django默認情況下,按字母順序?qū)δP瓦M行排序。因此,Event應(yīng)用模型的順序為Epic、EventHero、EventVillain、Event
假設(shè)你希望順序是
EventHero、EventVillain、Epic、Event。
用于呈現(xiàn)后臺indxe頁面的模板為admin/index.html,對應(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)
默認的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
因此,可以通過覆蓋get_app_list方法來修改顯示順序:
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']])用來設(shè)置默認順序。修改后效果如下。

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

