Django orm does not follow reverse relations. Why not?
I present example how this could be done:
#models:
from django.db import models
class Book(models.Model):
ISBN = models.IntegerField()
class Translation(models.Model):
title = models.CharField(max_length=32)
book = models.ForeignKey("Book")
language= models.CharField(max_length=4)
#views:
def index(request):
books = Book.objects.filter(translation__language="en",translation__title="1")
output = ', '.join([obj.translation_set.all()[0].title for obj in books])
return HttpResponse(output)
#what django does:SQL
SELECT "test3_book"."id", "test3_book"."ISBN" FROM "test3_book" INNER JOIN "test3_translation" ON ("test3_book"."id" = "test3_translation"."book_id") WHERE ("test3_translation"."language" <> 'bz' AND "test3_translation"."title" <> 'fdsfsd' )
SELECT "test3_translation"."id", "test3_translation"."title", "test3_translation"."book_id", "test3_translation"."language" FROM "test3_translation" WHERE "test3_translation"."book_id" = 1 LIMIT 1
# so if we have 100 objects it will do 100 additional queries
#change proposed :
#this would be just fine (just added Translation fields):
SELECT
"test3_book"."id",
"test3_book"."ISBN",
"test3_translation"."title", #this was added
"test3_translation"."language" #this was added
FROM "test3_book"
INNER JOIN "test3_translation"
ON ("test3_book"."id" = "test3_translation"."book_id")
WHERE ("test3_translation"."language" <> 'bz' AND "test3_translation"."title" <> 'fdsfsd' )
this means that if I will get 100 models, djanog will hit db 100 more times!
this can be avoided by adding the extra fields from related model to SELECT
Note that this is only example, what if Book has more than 1 related object, fetching by Translation makes no sense.