0PricingLogin
Django Academy · Lesson

order_by, Slicing, and Lazy Evaluation

Sort, limit, and understand when queries hit the DB.

Put Rows in Order

Database rows have no guaranteed order. Call order_by() to sort a QuerySet by any field you choose.

Book.objects.order_by("title")

Sort Descending

Put a minus sign in front of a field name to reverse the order, so the largest or newest value comes first.

Book.objects.order_by("-year")

All lessons in this course

  1. all(), get(), and filter()
  2. Field Lookups and exclude()
  3. order_by, Slicing, and Lazy Evaluation
  4. values, values_list, and count
← Back to Django Academy