英文:
How to set ordering of Apps and models in Django admin dashboard
问题
我想设置Django仪表板中应用程序模型列表的特定顺序。我的模型看起来像这样:
我尝试了几个教程,比如这个,并查看了Stack Overflow上其他人的解决方案,比如这些问题:https://stackoverflow.com/questions/4877335/how-to-use-custom-adminsite-class?noredirect=1&lq=1、https://stackoverflow.com/questions/58256151/set-ordering-of-apps-and-models-in-django-admin-dashboard、https://stackoverflow.com/questions/48293930/reorder-app-and-models-in-django-admin,但这些方法都不起作用,它们要么使用了第三方库,要么没有明确指定代码放在哪里。这个似乎最有希望,但我尝试将以下代码段放入我的应用程序的admin.py中,但它不起作用:
class WebsiteAdminSite(admin.AdminSite):
def get_app_list(self, request):
"""
返回在此站点中已注册的所有已安装应用程序的已排序列表。
"""
ordering = {
"Menues": 1,
"Entrees": 2,
"First_dishes": 3,
"Second_dishes": 4,
"Side_dishes": 5,
"Desserts": 6,
"Wines": 7,
"Opening_hours": 8,
"Contacts": 9,
"Gallery_images": 10,
}
app_dict = self._build_app_dict(request)
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
for app in app_list:
app['models'].sort(key=lambda x: ordering[x['name']])
return app_list
我无法提供错误信息,因为没有错误,它只是不起作用。我认为它应该工作,因为在Django 4.1中应该有可能重写AdminSite,但我无法使其工作。
我应该将上面的代码类放在哪里?还有其他什么我应该在admin.py中添加吗?像我在这个答案中找到的以下代码行https://stackoverflow.com/questions/58256151/set-ordering-of-apps-and-models-in-django-admin-dashboard:
mysite = WebsiteAdminSite()
admin.site = mysite
sites.site = mysite
这些代码行是做什么的?当我在类之后添加它们时,代码就会出错,由django.contrib.admin.sites.catch_all_view
引发错误。
更新
根据一位回答者的建议,我正在添加我的项目文件夹结构以及我所做的更改。我的项目文件夹现在如下所示:
-base
-base
admin.py
apps.py
asgi.py
settings.py
urls.py
wsgi.py
-website(应用程序文件夹)
-migrations
-templates
admin.py
apps.py
models.py
tests.py
urls.py
views.py
-dbsqlite3
manage.py
所以我在我的项目文件夹中添加了admin.py,其中包含我感兴趣的类,并在我的应用程序文件夹(website)的apps.py中添加了以下类:
class WebsiteAdminConfig(AdminConfig):
default_site = 'base.admin.WebsiteAdminSite'
并在settings.py中修改了INSTALLED_APPS,现在看起来像这样:
INSTALLED_APPS = [
'base.apps.WebsiteAdminConfig',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website',
]
我的应用程序文件夹(website)中的apps.py如下:
from django.apps import AppConfig
class WebsiteConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'website'
以及我的项目文件夹(base)中的apps.py:
from django.contrib.admin.apps import AdminConfig
class WebsiteAdminConfig(AdminConfig):
default_site = 'base.admin.WebsiteAdminSite'
现在我遇到了另一个问题,这一行:
app['models'].sort(key=lambda x: ordering[x['name']])
给我一个键错误'Groups'。
修复这个问题,我需要在WebsiteAdminSite类中的ordering字典中添加'Groups'和'Users':
ordering = {
"Menues": 1,
"Entrees": 2,
"First_dishes": 3,
"Second_dishes": 4,
"Side_dishes": 5,
"Desserts": 6,
"Wines": 7,
"Opening_hours": 8,
"Contacts": 9,
"Gallery_images": 10,
"Groups": 11,
"Users": 12,
}
经过这个修改,它应该可以正常工作。
英文:
i would like to set a specific order of the models list of an app in the django dashboard. my models look like this:
i've tried several tutorial like this one and looked other people solution inside SO like this questions https://stackoverflow.com/questions/4877335/how-to-use-custom-adminsite-class?noredirect=1&lq=1 https://stackoverflow.com/questions/58256151/set-ordering-of-apps-and-models-in-django-admin-dashboard https://stackoverflow.com/questions/48293930/reorder-app-and-models-in-django-admin
but none of these works, they either use a third party library or they don't specify where to put the pieces of code. This one seem to be the most promising but i tried to put the piece of code,
class WebsiteAdminSite(admin.AdminSite):
def get_app_list(self, request):
"""
Return a sorted list of all the installed apps that have been
registered in this site.
"""
ordering = {
"Menues": 1,
"Entrees": 2,
"First_dishes": 3,
"Second_dishes": 4,
"Side_dishes": 5,
"Desserts": 6,
"Wines": 7,
"Opening_hours": 8,
"Contacts": 9,
"Gallery_images": 10,
}
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
inside the admin.py of my app but it doesn't work.
i can't provide any error since none is given, it simply doesn't work. i think it should since in django 4.1 there should be the possibility to override AdminSite but i can't make it work.
Where should i put the class in the code above? is there anything else i should add in the admin.py? like the following lines of code i've found in this answerhttps://stackoverflow.com/questions/58256151/set-ordering-of-apps-and-models-in-django-admin-dashboard
mysite = WebsiteAdminSite()
admin.site = mysite
sites.site = mysite
what do these lines of code do? when i add them after the class i broke the code and an error appear raised by django.contrib.admin.sites.catch_all_view
UPDATE
following an answer i'm adding my project folder structure and the changes i've made.
my project folder look like this now:
-base
-base
admin.py
apps.py
asgi.py
settings.py
urls.py
wsgi.py
-website (the app folder)
-migrations
-templates
admin.py
apps.py
models.py
tests.py
urls.py
views.py
-dbsqlite3
manage.py
so i've added the admin.py inside my project folder with the class i am interested in, added the class
class WebsiteAdminConfig(AdminConfig):
default_site = 'base.admin.WebsiteAdminSite'
inside my apps.py in the app folder (website) and modified the INSTALLED_APPS in settings.py that now look like the following:
INSTALLED_APPS = [
'base.apps.WebsiteAdminConfig',
#'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'website',
]
my apps.py inside the app folder (website) look like this:
from django.apps import AppConfig
class WebsiteConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'website'
and the apps.py inside my project folder (base) :
from django.contrib.admin.apps import AdminConfig
class WebsiteAdminConfig(AdminConfig):
default_site = 'base.admin.WebsiteAdminSite'
now i have another problem, this line
app['models'].sort(key=lambda x: ordering[x['name']])
give me a key error 'Groups'
fixed this it should be functioning.
UPDATE2
to manage the error i needed to add "Groups" and "Users" inside the ordering dictionary inside the class WebsiteAdminSite:
ordering = {
"Menues": 1,
"Entrees": 2,
"First_dishes": 3,
"Second_dishes": 4,
"Side_dishes": 5,
"Desserts": 6,
"Wines": 7,
"Opening_hours": 8,
"Contacts": 9,
"Gallery_images": 10,
"Groups":11,
"Users":12,
}
答案1
得分: 1
以下是翻译好的部分:
- mysite
- mysite
- asgi.py
- settings.py
- urls.py
- wsgi.py
- app
- admin.py
- apps.py
- etc.
在上面的代码中,我应该将类放在哪里?
根据Django文档,它应该存储在您的项目中的mysite/admin.py
中,就像这样:
- mysite
- mysite
- admin.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
- app
- admin.py
- apps.py
- etc.
根据Django文档,您还需要在mysite/apps.py
中创建一个AdminConfig
类。
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'mysite.admin.MyAdminSite'
然后在mysite/settings.py
的INSTALLED_APPS
变量中用该配置类替换'django.contrib.admin'
。
INSTALLED_APPS = [
...
'mysite.apps.MyAdminConfig', # 替换 'django.contrib.admin'
...
]
阅读文档非常重要,不要匆忙跳过它。
在admin.py中还有其他需要添加的吗?
这取决于您想要自定义的其他内容。您不应该需要添加其他内容。如果您想要更改更多内容,请查看文档,或者在GitHub上查看源代码。
英文:
It would be helpful for you to post the structure of your project. Something similar to this.
- mysite
- mysite
- asgi.py
- settings.py
- urls.py
- wsgi.py
- app
- admin.py
- apps.py
- etc.
> Where should i put the class in the code above?
According to Django documentation, it should be stored in your projects in mysite/admin.py
. Like so:
- mysite
- mysite
- admin.py
- asgi.py
- settings.py
- urls.py
- wsgi.py
- app
- admin.py
- apps.py
- etc.
According to Django documentation, you will also need to create a AdminConfig
class in mysite/apps.py
.
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'mysite.admin.MyAdminSite'
And replace 'django.contrib.admin'
with said config class in the INSTALLED_APPS
variable in mysite/settings.py
.
INSTALLED_APPS = [
...
'mysite.apps.MyAdminConfig', # replaces 'django.contrib.admin'
...
]
Reading the documentation is important, don't rush over it.
> is there anything else i should add in the admin.py?
Depends on what else you want to customize. You shouldn't need to add anything else. Check the documentation if you want to change more things, or take a look at the source code on github.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论