[英]Reorder model objects in django admin panel
I have several configuration objects in django
admin panel. 我在
django
管理面板中有几个配置对象。 They are listed in the following order 它们按以下顺序列出
Each object can be configured separately, but all of them are included in General config
. 每个对象都可以单独配置,但所有对象都包含在
General config
。 So basically you will need mostly General config
, so I want to move it to the top. 所以基本上你需要大多数
General config
,所以我想把它移到顶部。 I know how to order fields in a model itself, but how to reorder models? 我知道如何在模型中订购字段,但如何重新排序模型?
So I've just wrote a django package that will allow you reorder django app list/ rename app label and model names (including 3rd party apps) — you can download it from here: 所以我刚刚写了一个django软件包,允许你重新订购django应用程序列表/重命名应用程序标签和模型名称(包括第三方应用程序) - 你可以从这里下载:
https://github.com/mishbahr/django-modeladmin-reorder https://github.com/mishbahr/django-modeladmin-reorder
我没有看到明显的解决方案 - 模型按照_meta.verbose_name_plural进行排序,这发生在AdminSite.index视图中,没有明显的地方可以挂钩自定义代码,缺少子类化AdminSite类并提供自己的索引方法,这是一个巨大的单片方法,非常继承 - 不友好。
If you don't mind using a dirty trick, prepend verbose_name_plural
of your models with a certain number of invisible zero-width spaces . 如果您不介意使用脏技巧,请使用一定数量的不可见零宽度空格前置模型的
verbose_name_plural
。 Eg prepend "Email config" with 1 zero-width space, "General config" with 2 and "Network config" with 3. This is really the simplest method and I've yet to find any shortcomings. 例如,将“电子邮件配置”添加到1个零宽度空间,“常规配置”添加2,“网络配置”添加3.这是最简单的方法,我还没有找到任何缺点。
I found this snippet for reordering models in the django admin panel - it works for us (though check out the comments below the snippet for updates to make it work with Django >= 1.4) 我在django管理面板中找到了重新排序模型的片段 - 它适用于我们(虽然查看下面的注释片段以获取更新以使其与Django一起使用> = 1.4)
And for the longer term there is this django bug report about the ordering of models within apps . 从长远来看,有关于应用程序中模型排序的django错误报告 。 The bug report is open at the time of writing this answer.
在撰写此答案时,错误报告已打开。
If you just want to reorder your apps (tested with Django 1.11.10
). 如果您只想重新排序应用程序(使用
Django 1.11.10
进行测试)。
class MyAdminSite(AdminSite):
def index(self, request, extra_context=None):
if extra_context is None:
extra_context = {}
# Move last app to the top of `app_list`.
app_list = self.get_app_list(request)
app_list.insert(0, app_list.pop(-1))
extra_context['app_list'] = app_list
return super().index(request, extra_context)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.