Skip to content

Instantly share code, notes, and snippets.

@kyle-eshares
Last active October 17, 2016 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyle-eshares/fb5d95d4111902d590c869d68c67cc29 to your computer and use it in GitHub Desktop.
Save kyle-eshares/fb5d95d4111902d590c869d68c67cc29 to your computer and use it in GitHub Desktop.
# Don't waste a query if you are using the queryset
books = Book.objects.filter(..)
if len(books) > 5:
do_stuff_with_books(books)
# If you aren't using the queryset use count
books = Book.objects.filter(..)
if books.count() > 5:
do_some_stuff()
# But never
if len(Book.objects.filter(..)) > 5:
do_some_stuff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment