Expanding on Jcc's answer and Luciano's comment, the solution for the exact OP's question would be the following.
Code
cheapest_choclate = Choclate.objects.values_list('name', 'price').order_by('price')[0]
lowest_price = cheapest_choclate['price']
cheapest_choclate_name = cheapest_choclate['name']
Explanation
Using Choclate.objects
, you obtain all objects of class Choclate from the DB.
Using .values_list('name', 'price')
, you create a QuerySet with a list of tuples where each tuple contains the 'name' and 'price' of each object - e.g. [('Sweet', 79),('Bitter', 49), ('Spicy', 69)]
.
Using .order_by('price')
, you order the tuples in the list using the price - e.g. [('Bitter', 49), ('Spicy', 69), ('Sweet', 79)]
.
Using [0]
, you select the first tuple from the list. This tuple contains the name and price of the cheapest choclate. You could also use .first()
as suggested by Jcc. However, I like the [0]
approach more as it can easily be changed to something else if needed - e.g. "second cheapest" choclate could be obtained using [1]
.
Lastly, you can obtain the price and name of the cheapest choclate from the first tuple using cheapest_choclate['price']
and cheapest_choclate['name']
.