我可以在Django Admin索引中显示每个人的最后操作吗?

时间:2014-03-12 21:19:21

标签: django django-admin

有没有办法显示Django管理员中的最后一个动作?默认情况下,admin仅显示当前用户的最后操作,但我希望查看每个管理员的最后操作。由于我的项目中没有此页面的任何代码,我该如何与此小部件进行交互?我应该覆盖整个索引吗?

我想得到这样的东西:

Result expected in the admin interface

如果我作为er****连接(根据屏幕),

而不是仅仅是第一个条目。

2 个答案:

答案 0 :(得分:6)

是的,确实如此。 Django管理员中的所有内容都可以通过覆盖模板进行自定义。您只需覆盖文件templates/admin/index.html of your current Django version并更改此行:

{% get_admin_log 10 as admin_log for_user user %}

并删除for_user user部分。它将显示最近10个最近的操作,而不会被用户过滤。为了完美,您还需要更改块的名称并添加动作作者。侧边栏应该是这样的:

{% block sidebar %}
<div id="content-related">
    <div class="module" id="recent-actions-module">
        <h2>{% trans 'Recent Actions' %}</h2>
        <h3>{% trans 'Last Actions' %}</h3>             {# Title modified #}
            {% load log %}
            {% get_admin_log 10 as admin_log %}         {# No more filtering #}
            {% if not admin_log %}
            <p>{% trans 'None available' %}</p>
            {% else %}
            <ul class="actionlist">
            {% for entry in admin_log %}
            <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">
                {% if entry.is_deletion or not entry.get_admin_url %}
                    {{ entry.object_repr }}
                {% else %}
                    <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>
                {% endif %}
                <br/>
                {% if entry.content_type %}
                    {# Add the author here, at the end #}
                    <span class="mini quiet">{% filter capfirst %}{% trans entry.content_type.name %}{% endfilter %}, by {{ entry.user }}</span>    
                {% else %}
                    <span class="mini quiet">{% trans 'Unknown content' %}</span>
                {% endif %}
            </li>
            {% endfor %}
            </ul>
            {% endif %}
    </div>
</div>
{% endblock %}

答案 1 :(得分:3)

这很容易在django 1.9上覆盖:

在加载管理界面的主人src中(使用urls.py),覆盖管理员索引模板文件的名称:

admin.autodiscover()

然后在任何应用程序的模板目录(例如from django.contrib import admin admin.site.index_template = 'admin/my_custom_index.html' admin.autodiscover() )内创建文件admin\my_custom_index.html。它可以扩展现有模板,因此不需要那么冗长:

\my_app\templates\admin\my_custom_index.html

多年来,这个版块在django中有所不同,这里的版本比Maximime的答案更新。