DGDD
DGDD

Reputation: 1390

Django - Is there a way to find the maximum/minimum value of a field from models without using an iterator?

For instance I have a model like so:

class Record(models.Model):
     name = CharField(...)
     price = IntegerField(...)
     year = IntegerField(...)

How can I find the minimum or maximum of year without using a for, or while iterator?

Upvotes: 3

Views: 5383

Answers (1)

Joseph Victor Zammit
Joseph Victor Zammit

Reputation: 15310

This doc is about how aggregation works in Django. For instance, for your Record model class you can compute the min and max value for price attribute using the following syntax:

from django.db.models import Min, Max

Record.objects.annotate(Min("price"), Max("price"))

Upvotes: 9

Related Questions