Skip to content

Instantly share code, notes, and snippets.

@abrorbekuz
Created January 5, 2025 11:10
Show Gist options
  • Select an option

  • Save abrorbekuz/75d1b7bf00553a4309ecf30551b805cf to your computer and use it in GitHub Desktop.

Select an option

Save abrorbekuz/75d1b7bf00553a4309ecf30551b805cf to your computer and use it in GitHub Desktop.
DJango Admin Panel Regroup
from django.contrib import admin
from copy import deepcopy
def update_skeleton(skeleton, update):
new_skeleton = skeleton.copy()
new_skeleton.update(update)
return new_skeleton
def get_app_list(self, request):
"""
Returns a customized app list for the admin index page.
Groups transaction-related models at the top and moves original entries to bottom.
"""
app_dict = self._build_app_dict(request)
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
# Define custom grouping
regroup = [
'''' Ex:
{
'title': 'TransactionALL',
'models': {
'transaction': ['transaction'],
'click_uz': ['transaction'],
'payme': ['paymetransactions']
},
'move_to_bottom': ['transaction', 'click_uz', 'payme'],
},
{
'title': 'NavigationALL',
'models': {
'category': ['category'],
'service': ['servicetype']
},
'move_to_bottom': ['category'],
},
'''
]
apps_with_regrouped = set()
for group in regroup:
for app_label in group['move_to_bottom']:
apps_with_regrouped.add(app_label)
prioritized_apps = []
deprioritized_apps = []
print(apps_with_regrouped)
for app in app_list:
if app['app_label'] in apps_with_regrouped:
deprioritized_apps.append(app)
else:
prioritized_apps.append(app)
skeleton = {
'name': '',
'app_label': '',
'app_url': '/admin',
'has_module_perms': True,
'models': []
}
new_grouped_apps = []
for group in regroup:
new_app = update_skeleton(skeleton, {
'name': group['title'],
'app_label': group['title'].lower(),
'app_url': f'/admin/{group["title"].lower()}/',
'models': []
})
for app in app_dict.values():
for model in app['models']:
if (app['app_label'] in group['models'] and
model['object_name'].lower() in group['models'][app['app_label']]):
new_app['models'].append(deepcopy(model))
new_grouped_apps.append(new_app)
final_app_list = new_grouped_apps + prioritized_apps + deprioritized_apps
return final_app_list
admin.AdminSite.get_app_list = get_app_list
@abrorbekuz
Copy link
Author

image

admin.py ni farqi yo'q

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment