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
- all(), get(), and filter()
- Field Lookups and exclude()
- order_by, Slicing, and Lazy Evaluation
- values, values_list, and count