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 How to use custom AdminSite class? Set ordering of Apps and models in Django admin dashboard 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,
}