Set ordering of Apps and models in Django admin dashboard

2024/10/13 20:18:08

By default, the Django admin dashboard looks like this for me:

enter image description here

I want to change the ordering of models in Profile section, so by using codes from here and here I was able to change the ordering of model names in Django admin dashboard:

class MyAdminSite(admin.AdminSite):def get_app_list(self, request):"""Return a sorted list of all the installed apps that have beenregistered in this site."""ordering = {"Users": 1,"Permissions": 2,"Activities": 3,}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_listmysite = MyAdminSite()
admin.site = mysite
sites.site = mysite

New look and feel:

enter image description here

But as you see, I have lost the AUTHENTICATION AND AUTHORIZATION section; What should I do to have all the sections and at the same time have my own custom ordering for Profile section?

Answer

With the code below in settings.py, you can reorder and hide the apps including AUTHENTICATION AND AUTHORIZATION(auth) and models in admin pages. *I use Django 4.1.7 and you can see the original get_app_list() in GitHub and you can see my answer demonstrating how the code below works:

# "settings.py"ADMIN_ORDERING = (('app2', ('Model3', 'Model1', 'Model2')),('auth', ('User', 'Group')),('app1', ('Model2', 'Model3', 'Model1'))
)from django.contrib import admindef get_app_list(self, request, app_label=None):app_dict = self._build_app_dict(request, app_label)if not app_dict:returnNEW_ADMIN_ORDERING = []if app_label:for ao in ADMIN_ORDERING:if ao[0] == app_label:NEW_ADMIN_ORDERING.append(ao)breakif not app_label:for app_key in list(app_dict.keys()):if not any(app_key in ao_app for ao_app in ADMIN_ORDERING):app_dict.pop(app_key)app_list = sorted(app_dict.values(), key=lambda x: [ao[0] for ao in ADMIN_ORDERING].index(x['app_label']))for app, ao in zip(app_list, NEW_ADMIN_ORDERING or ADMIN_ORDERING):if app['app_label'] == ao[0]:for model in list(app['models']):if not model['object_name'] in ao[1]:app['models'].remove(model)app['models'].sort(key=lambda x: ao[1].index(x['object_name']))return app_listadmin.AdminSite.get_app_list = get_app_list
https://en.xdnf.cn/q/69493.html

Related Q&A

python database / sql programming - where to start

What is the best way to use an embedded database, say sqlite in Python:Should be small footprint. Im only needing few thousands records per table. And just a handful of tables per database. If its one …

How to install Python 3.5 on Raspbian Jessie

I need to install Python 3.5+ on Rasbian (Debian for the Raspberry Pi). Currently only version 3.4 is supported. For the sources I want to compile I have to install:sudo apt-get install -y python3 pyth…

Django - last insert id

I cant get the last insert id like I usually do and Im not sure why.In my view:comment = Comments( ...) comment.save() comment.id #returns NoneIn my Model:class Comments(models.Model):id = models.Integ…

How to check if default value for python function argument is set using inspect?

Im trying to identify the parameters of a function for which default values are not set. Im using inspect.signature(func).parameters.value() function which gives a list of function parameters. Since Im…

OpenCV-Python cv2.CV_CAP_PROP_POS_FRAMES error

Currently, I am using opencv 3.1.0, and I encountered the following error when executing the following code:post_frame = cap.get(cv2.CV_CAP_PROP_POS_FRAMES)I got the following error Message:File "…

How to get the total number of tests passed, failed and skipped from pytest

How can I get to the statistics information of a test session in pytest?Ive tried to define pytest_sessionfinish in the conftest.py file, but I only see testsfailed and testscollected attributes on th…

Tensorflow ArgumentError Running CIFAR-10 example

I am trying to run the CIFAR-10 example of Tensorflow. However when executing python cifar10.py I am getting the error attached below. I have installed Version 0.6.0 of the Tensorflow package using pip…

How to plot multiple density plots on the same figure in python

I know this is going to end up being a really messy plot, but I am curious to know what the most efficient way to do this is. I have some data that looks like this in a csv file:ROI Band Min…

Running Tkinter on Mac

I am an absolute newbie. Im trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message: >>> import tkinter Traceback (most…

Object-based default value in SQLAlchemy declarative

With SQLAlchemy, it is possible to add a default value to every function. As I understand it, this may also be a callable (either without any arguments or with an optional ExecutionContext argument).No…