I followed anjaneyulubatta505 answer but it only changed the order in the index page to change the order in all admin pages
override app_list.html
not index.html
app the template tag from anjaneyulubatta505 answer in any app
from django import template
from django.conf import settings
register = template.Library()
def pop_and_get_app(apps, key, app_label):
for index, app in enumerate(apps):
if app[key] == app_label:
obj = apps.pop(index)
return obj
return None
@register.filter
def sort_apps(apps):
new_apps = []
order = settings.APP_ORDER
for app_label in order.keys():
obj = pop_and_get_app(apps, "app_label", app_label)
new_apps.append(obj) if obj else None
apps = new_apps + apps
for app in apps:
models = app.get("models")
app_label = app.get("app_label")
new_models = []
order_models = settings.APP_ORDER.get(app_label, [])
for model in order_models:
obj = pop_and_get_app(models, "object_name", model)
new_models.append(obj) if obj else None
models = new_models + models
app["models"] = models
return apps
and add the order in your settings.py
from collections import OrderedDict
APP_ORDER = OrderedDict([
("app1", ["Model2", "Model1", "Model3"]),
("app2", ["Model2", "Model5", "Model3"]),
("app3", ["Model1", "Model6", "Model3"]),
])
and this is my app_list.html
{% load i18n admin_tags %}
{% if app_list %}
{% for app in app_list|sort_apps %}
<div class="app-{{ app.app_label }} module{% if app.app_url in request.path|urlencode %} current-app{% endif %}">
<table>
<caption>
<a href="{{ app.app_url }}" class="section" title="{% blocktranslate with name=app.name %}Models in the {{ name }} application{% endblocktranslate %}">{{ app.name }}</a>
</caption>
{% for model in app.models %}
<tr class="model-{{ model.object_name|lower }}{% if model.admin_url in request.path|urlencode %} current-model{% endif %}">
{% if model.admin_url %}
<th scope="row"><a href="{{ model.admin_url }}"{% if model.admin_url in request.path|urlencode %} aria-current="page"{% endif %}>{{ model.name }}</a></th>
{% else %}
<th scope="row">{{ model.name }}</th>
{% endif %}
{% if model.add_url %}
<td><a href="{{ model.add_url }}" class="addlink">{% translate 'Add' %}</a></td>
{% else %}
<td></td>
{% endif %}
{% if model.admin_url and show_changelinks %}
{% if model.view_only %}
<td><a href="{{ model.admin_url }}" class="viewlink">{% translate 'View' %}</a></td>
{% else %}
<td><a href="{{ model.admin_url }}" class="changelink">{% translate 'Change' %}</a></td>
{% endif %}
{% elif show_changelinks %}
<td></td>
{% endif %}
</tr>
{% endfor %}
</table>
</div>
{% endfor %}
{% else %}
<p>{% translate 'You don’t have permission to view or edit anything.' %}</p>
{% endif %}