How to set ordering of Apps and models in Django admin dashboard

Question:

i would like to set a specific order of the models list of an app in the django dashboard. my models look like this:
how my models look like now

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,
        }

Asked By: LeoB

||

Answers:

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.

Answered By: eliaseraphim
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.