select_related optimization and enhancement for the django.contrib.admin

50 views
Skip to first unread message

David Cramer

unread,
Nov 10, 2008, 8:13:29 PM11/10/08
to Django developers
While I've been working on composite primary keys, I also made a few
tweaks to the admin app. Most of these are related to the primary key
support, but one is an optimization.

I would post this as a patch, or a ticket, but I want to open this up
for discussion, and my django repo is so far from trunk it's a PITA at
the moment to create the patch.

Anyways, what it does:

* list_select_related can be a boolean, or a list. If it's a list it
says "select_related on these fields"
* select_related is smart by default, no more implicit select all

If someone wants to create a patch, you'll be able to get to it a lot
faster than I will. Otherwise I'll eventually throw one up on trac for
each change.

line 198ish of django.contrib.admin.views.main

if isinstance(self.list_select_related, (tuple, list)):
qs = qs.select_related(*self.list_select_related)
elif self.list_select_related:
qs = qs.select_related()
else:
fields = []
for field_name in self.list_display:
try:
f = self.lookup_opts.get_field(field_name)
except models.FieldDoesNotExist:
pass
else:
if isinstance(f.rel, models.ManyToOneRel):
fields.append(name)
if fields:
qs = qs.select_related(*fields)

Malcolm Tredinnick

unread,
Nov 11, 2008, 4:17:24 AM11/11/08
to django-d...@googlegroups.com

On Mon, 2008-11-10 at 17:13 -0800, David Cramer wrote:
[...]

> Anyways, what it does:
>
> * list_select_related can be a boolean, or a list. If it's a list it
> says "select_related on these fields"

How is that different functionality from just specifying the names of
the fields in the current API? Are you just proposing a different API to
achieve the same thing, or is there an addition here?

> * select_related is smart by default, no more implicit select all

Backwards incompatible and I don't really see the need for it (can you
motivate why you're proposing this change?). Strong -1 for those reasons
(particularly the backwards incompatibility).

Regards,
Malcolm


David Cramer

unread,
Nov 11, 2008, 11:33:29 AM11/11/08
to django-d...@googlegroups.com
This change is here because when you say "this foreign key is in list_filter" it immediately does a select_related() (grabbing every single relation which is ridiculous). This change says "only grab the foreign keys which are used. The only addition is that list_select_related can be a boolean, or a list. This, in my eyes, is fully backwards compatible, as its only an addition to the public API, and a bug fix.
--
David Cramer
Director of Technology
iBegin
http://www.ibegin.com/
Reply all
Reply to author
Forward
0 new messages