Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » how do I tell the admin interface to do lookups with select_related? [RSS Feed]

#1 Nov. 5, 2005 17:44:16

Le R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how do I tell the admin interface to do lookups with select_related?


I have a foreign key to the auth.User model in one of my models (the
'owner' of the object) and I want to display the full name of the
person instead of the username in the listing for my model in the
admin interface.

Currently it displays the username, because that's what
auth,User.__repr__ returns, I assume. I don't want to update auth,
because every time I svn up it will override it, so I opted for a
method in list_display. Like this:

...
admin = meta.Admin(
list_display = ("name", "short_description",
"get_owner_fullname", "created")
)
...
def get_owner_fullname(self):
owner = self.get_owner()
return owner.first_name + " " + owner.last_name

get_owner_fullname.short_description = "Owner Name"
...

Is there a way to tell the admin interface to pass select_related=True
to the lookup for the listing? Then I don't have to do a query for
each row in the listing and I can just somehow use the owner object
directly.

Le Roux

Offline

#2 Nov. 5, 2005 18:15:40

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how do I tell the admin interface to do lookups with select_related?


On 11/5/05, Le Roux Bodenstein <> wrote:
> admin = meta.Admin(
> list_display = ("name", "short_description",
> "get_owner_fullname", "created")
> )
> ...
> def get_owner_fullname(self):
> owner = self.get_owner()
> return owner.first_name + " " + owner.last_name
>
> get_owner_fullname.short_description = "Owner Name"
> ...
>
> Is there a way to tell the admin interface to pass select_related=True
> to the lookup for the listing? Then I don't have to do a query for
> each row in the listing and I can just somehow use the owner object
> directly.

Yes! As of one minute ago, I added a "list_select_related" option to
meta.Admin. Thanks for the great suggestion.

Note that Django automatically adds select_related=True to its query
on the "change list" page if the list_display includes a ForeignKey
field. As you noted, though, using a custom method
("get_owner_fullname") doesn't tell Django that you're using a
ForeignKey.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Nov. 5, 2005 18:50:22

Le R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

how do I tell the admin interface to do lookups with select_related?


Thanks! wow, that was quick!

I can see the query is selecting a lot more stuff, but when I do a
self.get_owner() inside my function, it still run a query.. self.owner
is not already set either.

Surely it shouldn't run the query?

Le Roux

Offline

  • Root
  • » Django
  • » how do I tell the admin interface to do lookups with select_related? [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support