Custom|Custom models display ordering for Django admin dashboard
Django Admin
By default, it displays all the apps in INSTALLED_APPS that have been registered with the admin application, in alphabetical order.
https://docs.djangoproject.co...
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
https://github.com/django/dja...There are multiple ways to achieve this on the internet
- Adding a numeric prefix for each verbose_name_plural in App Model Meta
- Override admin/base.html tempalte
- Override AdminSite class and get_app_list() method
How to do it? 【Custom|Custom models display ordering for Django admin dashboard】Define the class method normally.
def get_app_list(self, request):
"""
Return a sorted list of all the installed apps that have been
registered in this site.
"""
# Retrieve the original list
app_dict = self._build_app_dict(request)
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())# Sort the models customably within each app.
for app in app_list:
if app['app_label'] == 'auth':
ordering = {
'Users': 1,
'Groups': 2
}
app['models'].sort(key=lambda x: ordering[x['name']])return app_list
Some things to take note,
- Retrieve the original list
- Define the ordering for each app, as the model names are different
- I am using app_lable to specify which app to apply
- The medel name is
verbose_name_plural or verbose_name
admin.AdminSite.get_app_list = get_app_list
推荐阅读
- ***|*** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- iOS|iOS CAShapeLayer+CADisplayLink 波浪动画
- 030Flutter|(九)Flutter CustomScrollView 之 SliverGrid与SliverList
- Deep Learning Models and Code for Pose Estimation
- flutter NestedScrollView 与其body中CustomScrollView滑动冲突解决
- inline-block
- CustomViewPager|CustomViewPager 穷途末路时试试这个
- 论文|预训练模型综述2020年三月《Pre-trained Models for Natural Language Processing: A Survey》
- 60个国外免费3D模型下载网站
- 层叠样式与盒子模型