Django help required with non foreign key relations

1,900 views
Skip to first unread message

Aditya Sriram M

unread,
Apr 22, 2012, 6:31:17 AM4/22/12
to django...@googlegroups.com

File myapp/models.py has this sample code..

from django.db import models

# model for 'user' table in database oracle_dbuser1:user
class User(models.Model):
   
. . .
    customerid
= models.BigIntegerField()

# model for 'customer' table in database oracle_dbuser2:customer
# Note that there is no Foreign key integrity among these legacy tables.
class Customer(models.Model):
   
. . .
    customerid
= models.BigIntegerField()

and the file myapp/admin.py has the following code:

from maasusers.models import User, Customer
from django.contrib import admin

class UserAdmin(admin.ModelAdmin):
   
# A handy constant for the name of the alternate database.
    db_one
= 'dbuser1'
    db_two
= 'dbuser2'

   
# display in a list
    list_display
= (. . .) # question 1

   
def queryset(self, request):
        result
= super(UserAdmin, self).queryset(request).using(self.db_one) # question 2
       
return result

# Register the Poll class
admin
.site.register(User, UserAdmin)
admin
.site.register(Customer, UserAdmin)

Question 1: Refer above: I want to display columns of both the tables. How can I achieve this? Eg.Select usr.col1, usr.col2, cust.col1, cust.col10 from user usr, customer cust where usr.col2 = cust.col3;

Question 2: How to write a corresponding queryset() function using the using function?

akaariai

unread,
Apr 22, 2012, 12:00:41 PM4/22/12
to Django users
There is no way to do a join between models if there is no relation
between them. I see two ways forward: modify one of the models to have
a foreign key - even if there is no foreign key in the DB, you can
have one in the models - or do the join in Python (fetch all
customers, then fetch users by
User.objects.filter(customerid__in=[obj.customerid for obj in
customers]). Finally join users to its customer).

- Anssi

Aditya Sriram M

unread,
Apr 23, 2012, 1:15:46 AM4/23/12
to django...@googlegroups.com
Ohh this looks little promising..

However,
  1. I need a few cols of table1 and few from table 2 to be displayed. How can we achieve that at the .filter() level? and 
  2. At the view level like using the list_display () for displaying them in the Admin interface

akaariai

unread,
Apr 23, 2012, 4:39:23 AM4/23/12
to Django users
On Apr 23, 8:15 am, Aditya Sriram M <aditya.cr3...@gmail.com> wrote:
> Ohh this looks little promising..
>
> However,
>
>    1. I need a few cols of table1 and few from table 2 to be displayed. How
>    can we achieve that at the .filter() level? and
>    2. At the view level like using the list_display () for displaying them
>    in the Admin interface

If you want to get results just define the ForeignKey (or OneToOneKey)
in your Customer or User model. After this .select_related
and .prefetch_related should solve the problems you are currently
facing.

If you can't define that ForeignKey for one reason or another you need
a lot more knowledge of how Django works. All the things you need to
do are too numerous to handle in a django-users post. It is possible
there will be some problems which are very hard to solve along this
path.

In short, I recommend to just define the relation between the models.

- Anssi
Reply all
Reply to author
Forward
0 new messages