I have a list of objects how can I run a query to give the max value of a field:
I'm using this code:
def get_best_argument(self):
try:
arg = self.argument_set.order_by('-rating')[0].details
except IndexError:
return 'no posts'
return arg
rating is an integer
See this. Your code would be something like the following:
from django.db.models import Max
# Generates a "SELECT MAX..." statement
Argument.objects.all().aggregate(Max('rating'))
If you've already gotten the list out of the database, then you'd do something like this:
from django.db.models import Max
args = Argument.objects.all()
# Calculates the maximum out of the already-retrieved objects
args.aggregate(Max('rating'))
This is untested so I may have made a mistake somewhere, but there you go.
Django also has the 'latest(field_name = None)' function that finds the latest (max. value) entry. It not only works with date fields but also with strings and integers.
You can give the field name when calling that function:
max_rated_entry = YourModel.objects.latest('rating')
return max_rated_entry.details
Or you can already give that field name in your models meta data:
from django.db import models
class YourModel(models.Model):
#your class definition
class Meta:
get_latest_by = 'rating'
Now you can call 'latest()' without any parameters:
max_rated_entry = YourModel.objects.latest()
return max_rated_entry.details
I've tested this for my project, it finds the max/min in O(n) time:
from django.db.models import Max
#Find the maximum value of the rating, and then get the record with that rating. Notice the double underscores in rating__max
max_rating = App.objects.all().aggregate(Max('rating'))['rating__max']
return App.objects.get(rating = max_rating)
This is guaranteed to get you one of the maximum elements efficiently, rather than sorting the whole table and getting the top (around O(nlogn)).