Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Last active April 28, 2020 18:25
Show Gist options
  • Save susmithagudapati/5c885444146fd977eef08781b64dc701 to your computer and use it in GitHub Desktop.
Save susmithagudapati/5c885444146fd977eef08781b64dc701 to your computer and use it in GitHub Desktop.
# retrieving products with all fields
%timeit Product.objects.all()
6.08 µs ± 97.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# fetching products after deferring name and category
%timeit Product.objects.defer('name', 'category')
14.4 µs ± 103 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# more about using 'defer()'
# defers both name and attributes columns
Product.objects.defer("name").filter(stock__gt=5).defer("attributes")
# defer also works on related object fields
Product.objects.select_related().defer("category__created_by")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment