How to reassign a model to a different app for dis

2019-08-22 03:46发布

I know I can change the display title for a model in Django Admin using

    class Meta:
        verbose_name='Custom Model Name Here'

However, is there a way to display which app heading a model is displayed under?

For example, if I create a custom user model Users in a new app also called users then the default user model goes from Authentication and Authorization > Users to Users > Users.

I would like to retain it under the original heading Authentication and Authorization > Users.

enter image description here

I have read this answer which suggests changes the app verbose_name, however it only changes the verbose name of the app associated with the model. I want to show the model in a different group on the admin panel. You can see the issue that approach takes here:

enter image description here

1条回答
霸刀☆藐视天下
2楼-- · 2019-08-22 04:31

There is no simple way of doing what you're intending, check out the Django code/template that generates the view:

@never_cache
def index(self, request, extra_context=None):
    """
    Display the main admin index page, which lists all of the installed
    apps that have been registered in this site.
    """
    app_list = self.get_app_list(request)

    context = {
        **self.each_context(request),
        'title': self.index_title,
        'app_list': app_list,
        **(extra_context or {}),
    }

    request.current_app = self.name

    return TemplateResponse(request, self.index_template or 'admin/index.html', context)

templates/admin/index.html:

{% for app in app_list %}
    <div class="app-{{ app.app_label }} module">
    <table>
    <caption>
        <a href="{{ app.app_url }}" class="section" title="{% blocktrans with name=app.name %}Models in the {{ name }} application{% endblocktrans %}">{{ app.name }}</a>
    </caption>
    {% for model in app.models %}
        <tr class="model-{{ model.object_name|lower }}">
        {% if model.admin_url %}
            <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
        {% else %}
            <th scope="row">{{ model.name }}</th>
        {% endif %}

        {% if model.add_url %}
            <td><a href="{{ model.add_url }}" class="addlink">{% trans 'Add' %}</a></td>
        {% else %}
            <td>&nbsp;</td>
        {% endif %}

        {% if model.admin_url %}
            {% if model.view_only %}
            <td><a href="{{ model.admin_url }}" class="viewlink">{% trans 'View' %}</a></td>
            {% else %}
            <td><a href="{{ model.admin_url }}" class="changelink">{% trans 'Change' %}</a></td>
            {% endif %}
        {% else %}
            <td>&nbsp;</td>
        {% endif %}
        </tr>
    {% endfor %}
    </table>
    </div>
{% endfor %}

As you can see, Django admin template is looping your app_list directy, so the only way of doing it would be to override the admin/index.html template to place your models in your desired order.

From Django docs:

For those templates that cannot be overridden in this way, you may still override them for your entire project. Just place the new version in your templates/admin directory. This is particularly useful to create custom 404 and 500 pages.

https://docs.djangoproject.com/en/2.2/ref/contrib/admin/#overriding-vs-replacing-an-admin-template

查看更多
登录 后发表回答