Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Last active April 27, 2020 11:22
Show Gist options
  • Save susmithagudapati/3bb6afe542bb9e95008f1828480d793c to your computer and use it in GitHub Desktop.
Save susmithagudapati/3bb6afe542bb9e95008f1828480d793c to your computer and use it in GitHub Desktop.
# instantiates a queryset and cache is empty.
product = Product.objects.get(id=5)
# Category object is retrieved from the database at this point
# and the results are saved in queryset's cache.
product.category
# retrieved from the cached results.
product.category
# callable attributes queries database every sigle time
product.categories.all() # query performed using db
product.categories.all() # query performed again using db
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment