#django #pattern #python
select_related provide us with a mechanism to look up objects related to the model that we're querying. We use
prefetch_related when we want to fetch a reverse foreign key or a many to many relationship. We use select related when we want to fetch a foreign key or a one to one relationship.
1posts_with_tags = BlogPost.objects.prefetch_related( 2 "tags" 3).get(id=5)
In this example, we're fetching a single blog post with a specific
slug, and with this post we're fetching all of the related tags. This helps us optimize our database queries by loading all the related tags instead of fetching them one by one.
prefetch_related, if we looped over
tags.all(), each iteration would result in a database query, but with
prefetch_related, a loop would result in one database query.
1latest_blog_posts = BlogPost.objects.select_related( 2 "blog" 3).order_by('-published_on')[:15]
select_related works similarly to
prefetch_related except we use
select_related for different types of relationships. For this case, we're fetching the latest blog post and also fetching the associated blog details.
Once again if we loop over
latest_blog_posts, referencing a blog post's blog details won't result in an extra database query.
The order in which you specify
select_related are not important. The following two queries are exactly the same:
You can enable SQL logging if you want to see what's happening behind the scenes.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.