django select_related - when to use it

前端 未结 4 1250
挽巷
挽巷 2020-12-08 04:54

I\'m trying to optimize my ORM queries in django. I use connection.queries to view the queries that django generate for me.

Assuming I have these models:

<         


        
4条回答
  •  囚心锁ツ
    2020-12-08 05:05

    Book.objects.select_related("author")
    

    is good enough. No need for Author.objects.all()

    {{ book.author.name }}
    

    won't hit the database, because book.author has been prepopulated already.

提交回复
热议问题