Django Admin设置应用程序及模型顺序方法详解

作者:临渊 时间:2021-01-18 14:45:18 

Django默认情况下,按字母顺序对模型进行排序。因此,Event应用模型的顺序为Epic、EventHero、EventVillain、Event

假设你希望顺序是

EventHero、EventVillain、Epic、Event。

用于呈现后台indxe页面的模板为admin/index.html,对应的视图函数为 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方法用于设置模型的顺序。


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']])用来设置默认顺序。修改后效果如下。

Django Admin设置应用程序及模型顺序方法详解

来源:https://www.cnblogs.com/superhin/p/12192535.html

标签:Django,Admin,应用,程序,模型
0
投稿

猜你喜欢

  • Python绘制全球疫情变化地图的实例代码

    2022-08-27 07:06:46
  • 基于python实现从尾到头打印链表

    2023-07-02 21:24:34
  • 什么是XML?

    2007-10-29 12:53:00
  • django中F与Q查询的使用

    2022-11-20 19:23:22
  • 在Yii框架中使用PHP模板引擎Twig的例子

    2023-11-14 11:30:30
  • Oracle9i在Win2k环境下的完全卸载

    2010-07-28 13:03:00
  • 用python发送微信消息

    2022-04-14 07:16:55
  • Django REST framework内置路由用法

    2022-04-06 14:22:35
  • 大家都说好用的Python命令行库click的使用

    2023-05-17 04:00:15
  • Python多线程应用于自动化测试操作示例

    2021-04-09 19:20:58
  • 详解Python的循环结构知识点

    2021-09-30 11:42:03
  • Anaconda+VSCode配置tensorflow开发环境的教程详解

    2021-04-03 09:47:32
  • 聊聊python中not 与 is None的区别

    2023-01-18 22:20:47
  • python3利用ctypes传入一个字符串类型的列表方法

    2021-06-10 20:33:36
  • Python实战之疫苗研发情况可视化

    2023-08-19 15:29:35
  • 最新LOGO设计流行趋势——叶子

    2007-10-02 18:26:00
  • 网站LOGO设计规范的思考--2.网络LOGO的设计

    2007-10-14 11:02:00
  • MySQL的存储过程写法和Cursor的使用

    2008-12-03 15:55:00
  • AJAX缓存问题的两种解决方法(IE)

    2008-05-02 20:57:00
  • 通过数据库引擎来加速的MySQL数据库

    2012-01-29 18:07:09
  • asp之家 网络编程 m.aspxhome.com