Trouble with admin select related

151 views
Skip to first unread message

Trey

unread,
Feb 19, 2009, 7:10:08 PM2/19/09
to Django users
I'm not sure if other people have stumbled onto this but I am having a
LOT of trouble getting the admin to use select related.

I have FK's that are nullable, this I know doesn't by default work
with select_related. But I've overridden the manager to select the
related fields I need (which I pretty much always need) and I still
can't get it to work. Here is my model def and the manager, if someone
could tell me if I am doing something stupid that would be awesome.

What's most frustrating is that select_related does not error or
interrupt in any way, so it seems as if it would work.

class PageManager(models.Manager):
def get_query_set(self):
return super(PageManager, self).get_query_set().select_related
('context__data_type')

class Page(models.Model):
objects = PageManager()
location = models.ForeignKey(Location, blank=True, null=True)
template = models.ForeignKey(Template, blank=True, null=True)
...
context = models.ForeignKey(DataObject, blank=True, null=True)

class DataObject(models.Model):
data_type = models.ForeignKey(DataObjectType)
...

class DataObjectType(models.Model):
...

Beres Botond

unread,
Feb 20, 2009, 5:46:14 AM2/20/09
to Django users
It doesn't work if you set up the list_select_related option
explicitly in the ModelAdmin
for Page?
Or if you add the foreign key field to the list_display list.

http://docs.djangoproject.com/en/dev/ref/contrib/admin/

Also have you tried just setting select_related() first, without any
field options, and see if it does something?

Karen Tracey

unread,
Feb 20, 2009, 12:15:08 PM2/20/09
to django...@googlegroups.com
On Thu, Feb 19, 2009 at 7:10 PM, Trey <james...@gmail.com> wrote:

I'm not sure if other people have stumbled onto this but I am having a
LOT of trouble getting the admin to use select related.

You know select_related is a performance optimization, right?  It doesn't affect results in any way, just the number of database queries that may be required to produce a page.  It isn't clear from what you have written here how you are determining that select_related is not working.  Are you checking the number of queries issued somehow and seeing more than you would expect?  If so, explicitly listing the queries you are seeing vs. what you are expecting (which also might require more complete model and admin defs so people can understand the queries) would help people either explain why the queries that are being issued are correct, or point out how to adjust the admin defs to get the results you are expecting.

If the problem is you are not seeing the results you are expecting on the admin pages, then it likely has nothing to do with select_related, and you need to more completely explain what you are expecting to see versus what you are seeing.

Karen

Trey

unread,
Feb 20, 2009, 3:45:22 PM2/20/09
to Django users
This was actually the admin overriding the select related in my
manager with an empty one, which does not follow FK=null.

Adding this to my Admin class I was able to force the select_related
to behave.

def queryset(self, request):
return super(PageAdmin, self).queryset(request).select_related
('context__data_type', 'location')

I think this approach is a little unintuitive and will start a convo
on the dev list.

On Feb 20, 12:15 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages