tags:

views:

102

answers:

2
queryObj = Rating.objects.select_related(
    'Candidate','State','RatingCandidate','Sig','Office','OfficeCandidate').get(
        rating_id = ratingId, 
        ratingcandidate__rating = ratingId,
        ratingcandidate__rating_candidate_id = \
             officecandidate__office_candidate_id)

This line gives me an error. I'm trying to get many different tables that are linked by primary keys and regular ids. The last selection is the problem:

ratingcandidate__rating_candidate_id = officecandidate__office_candidate_id.  

I need to skip around to get all the data.

+1  A: 

I'm trying to get many different tables that are linked by primary keys and regular ids.

Don't try to "join" tables. This isn't SQL.

You have to do multiple gets to get data from many different tables.

Don't worry about select_related until you can prove that you have a bottle-neck.

Just do the various GETs from the various classes as needed.

Let's focus on Candidate and Rating.

class Rating( Model ):
    ...

class Candidate( Model ):
    rating = Models.ForeignKey( Rating )

Do this.

r = Rating.objects.get( id=rating_id )
c = r.candidate_set().all()

This will get the rating and all the candidates that have that rating. This is -- in effect -- what a SQL join is: it's two fetches. In the Django ORM, just write the two fetches as simply as possible. Let Django (and your database) cache things for you.

To display elements of multiple tables in a single row on a template form, you do this.

In the view:

r = Rating.objects.get( id=rating_id )
return render_to_response( some_form, { 'rating':r } )

In the template:

Rating: {{rating}}.  Candidates: {% for c in rating.candidate_set.all %} {{c}} {%endfor%}

Etc.

You simply "navigate" among your objects in your template to display the requested information.

S.Lott
I didn't include the rest of the tables that I need values from. Once I create all the objects and have all the different data how would I merge it and output it in rows?
atomical
If I have a table that has multiple entries that match up with the return value(s) of Rating objects then it would be acceptable to fetch the whole table and put it into the template?
atomical
@atomical: What else can you do except fetch multiple entries and put them in the template? Are you **not** going to display multiple entries? I don't get the question.
S.Lott
Let's say you have 5 rating objects with a column named age. Another table named Sig has 1,000,000 rows total and 5 rows with age = 5. You want to take the values from the 5 rating objects and match them with Sig's age column. Would you pull those 1,000,000 rows and then put everything together in the template from the different objects?
atomical
@atomical: That's not how the Django ORM works. You follow the ForeignKey relationships. Django fetch relevant rows based on foreign keys. You don't join. You navigate from object to objects. Django ORM fetches the objects to which you are navigating. Not ALL, but ALL with the proper foreign key. Please read and do the entire Django tutorial. Please.
S.Lott
The tutorial is great if you work with databases that have a few tables, but it's limited for larger scale applications.
atomical
@atomical: "limited for larger scale applications"? We have hundreds of tables many with tens of thousands of rows. We simply use simple Django ORM navigation in the simplest way possible. I think it scales delightfully.
S.Lott
I was referring to the limits of the tutorial. The analogy I would use would be to take a three line example from a book on forking and then trying to use that example to construct an enterprise application.
atomical
@atomical: I don't get the analogy. You need to understand the Django ORM by writing queries. You cannot understand ORM by insisting that it do SQL joins for you. Your comments indicate that you have not written a single Django view function that retrieves from multiple tables. Your comments indicate that you need to spend more time with the Django ORM. Hence my suggestion to do the entire tutorial before asking questions that are pretty self-evident after you've worked with Django.
S.Lott
"The TUTORIAL is great if you work with databases that have a few tables, but it's limited for larger scale applications."My sentence was about the scope of the tutorial. Your reply was generally about Django and the particular merits of the framework where as I was talking specifically about the tutorial. It's all about context.
atomical
@atomical: "It's all about context" I'm not sure what this means. My point is this. It does not appear that you've made use of the tutorial. I don't care if you think ill of the tutorial or hate tutorials or think that tutorials can't help you because your problems is so far above and beyond the tutorial. Your comments indicate you do not "get" how ORM works. Please follow the tutorial so you give yourself an opportunity to "get" ORM.
S.Lott
A: 

You can't use the double-underscore syntax on its own on the right-hand side of the expression. If you need to reference field names on the right-hand side, use the F() function:

ratingcandidate__rating_candidate_id = F('officecandidate__office_candidate_id')
Daniel Roseman
So is this a form of join?
atomical
No. That's a query string to find the Rating object that matches all your criteria. There's no "Join" in Django's ORM.
S.Lott