﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
9083	get_admin_log tag improvement to prevent extra sql queries	santip	nobody	"I've noticed that the default index page for the admin site uses the get_admin_log tag to load the 10 most recent changes:

This the template snippet that shows that
{{{
            {% load log %}
            {% get_admin_log 10 as admin_log for_user user %}
            {% if not admin_log %}
            <p>{% trans 'None available' %}</p>
            {% else %}
            <ul class=""actionlist"">
            {% for entry in admin_log %}
                ... part of the snippet removed for clarity ...
                {% trans entry.content_type.name %}    <-- here's the access to content_type 
                                                           that causes a query in each step of the loop
                ... part of the snippet removed for clarity ...
            {% endfor %}
            </ul>
            {% endif %}
}}}

This causes 11 sql queries because even though the tag uses select_related() on the queryset, this doesn't join the content_type foreign key because it has blank=True and that results in a LEFT OUTER JOIN. I understand that this is a reasonable behavior for select_related() so my change doesn't change that but changes the tag implementation to explicitly define the fields to be joined.

It's a fairly simply change which automatically reduces the number of queries of the main admin page by 10, I'm pretty sure it doesn't have any negative effects since I haven't found any other use of this tag that wouldn't require the content_type foreign key to be loaded.

If the default behavior of select_related isn't the one it is supposed to have, then this fix shouldn't be included and a fix to that functionality should be introduced, if this is the case, I'm willing to look into it and create a different patch. Also if it is the expected behavior I think it should be properly documented, once this ticket is closed I'll submit another one with the modification to the documentation.

I'm adding a patch that should suffice if you agree with my remarks.  "		closed	contrib.admin	dev		fixed	LogEntry get_admin_log select_related content_type		Unreviewed	1	0	0	0	0	0
