Join tables with django

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 am trying to get a lot of different tables that are related by primary keys and regular identifiers. The final choice is the problem:

ratingcandidate__rating_candidate_id = officecandidate__office_candidate_id.  

I need to skip to get all the data.

+3
source share
2 answers

I am trying to get a lot of different tables that are related by primary keys and regular identifiers.

Do not try to join tables. This is not SQL.

You need to make several queries to get data from different tables.

Do not worry about select_relateduntil you can prove that you have a throat.

Just do different GETs from different classes as needed.

.

class Rating( Model ):
    ...

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

.

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

, . - - SQL-: . ORM Django . Django ( ) .

, .

:

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

:

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

Etc.

"" , .

+12

. , F():

ratingcandidate__rating_candidate_id = F('officecandidate__office_candidate_id')
+2

Source: https://habr.com/ru/post/1756217/


All Articles