DB joining 3 tables in django admin, while searching on the primary table

106 views
Skip to first unread message

Aditya Sriram M

unread,
May 19, 2012, 10:57:57 AM5/19/12
to django...@googlegroups.com
Hi,

again, my models are Customer, Users and Devices.

I would like to search by Customer and retrieve all 'select_related' rows of all the three models.
Eg. like this..

Customer1 User1 Device1
Customer1 User1 Device2
Customer1 User2 Device1
Customer1 User2 Device2
etc etc..

Is this possible in Django? If so how?

Matt Schinckel

unread,
May 19, 2012, 9:42:54 PM5/19/12
to django...@googlegroups.com
You may be able to use prefetch_related to do what you want: it will all depend upon your model structure. select_related is easier, but only follows an fk in one direction.

Matt.

Aditya Sriram M

unread,
May 20, 2012, 3:42:17 AM5/20/12
to django...@googlegroups.com
Ooo.. Nice.

Hmm.. I tried this but it failed? Can you pls correct me?

def queryset(self, request):
        result = super(CustomerAdmin, self).queryset(request).prefetch_related()
        return result

and my 'display list' for customer admin is..

list_display = ('get_user_name')

where definition of 'get_user_name' is

def get_user_name(self, obj):
        return u'%s %s %s' % (obj.customerid__first_name, obj.customerid__middle_name, obj.customerid__last_name)
get_user_name.short_description = 'User Name' 

where 'first_name', 'middle_name' etc are properties of the 'User' model.

Regards,
Aditya

Aditya Sriram M

unread,
May 21, 2012, 9:30:37 AM5/21/12
to django...@googlegroups.com
Some one there can answer!?

Derek

unread,
May 22, 2012, 10:10:56 AM5/22/12
to Django users
Please read this:
https://code.djangoproject.com/wiki/UsingTheMailingList#Donts:
before continuing to post these types of messages.

akaariai

unread,
May 22, 2012, 10:36:31 AM5/22/12
to Django users
On May 22, 5:10 pm, Derek <gamesb...@gmail.com> wrote:
> Please read this:https://code.djangoproject.com/wiki/UsingTheMailingList#Donts:
> before continuing to post these types of messages.

In addition, doing a "django prefetch_related" search on Google might
lead to some clues here...

- Anssi

bruno desthuilliers

unread,
May 23, 2012, 5:22:42 AM5/23/12
to Django users


On May 20, 9:42 am, Aditya Sriram M <aditya.cr3...@gmail.com> wrote:

> I tried this but it failed? Can you pls correct me?

yes : "it failed" is the worst possible way to describe a problem if
you expect to get some help. If you get an exception and traceback,
please read them carefully, and eventually post them. Else at least
describe the (unexpected) result.

> where definition of 'get_user_name' is
>
> def get_user_name(self, obj):
>         return u'%s %s %s' % (obj.customerid__first_name,
> obj.customerid__middle_name, obj.customerid__last_name)

What do you expect "obj.customerid__first_name" to eval to exactly ?

> where 'first_name', 'middle_name' etc are properties of the 'User' model.


http://en.wikipedia.org/wiki/Programming_by_permutation ?

I'm sorry to have to say, but it definitly looks like you just don't
have a clue about how Python and Django work. Please take time to
learn at least Python 101, then do Django tutorial. This should answer
your questions and save everyone time on such issues.

Reply all
Reply to author
Forward
0 new messages