Django - custom queryset

Viewed 31

I have a table like this:

class myTable(models.Model):  
    a = models.IntegerField(blank = True, default = 0)
    b = models.IntegerField(blank = True, default = 0)
    c = models.IntegerField(blank = True, default = 0)
    d = models.IntegerField(blank = True, default = 0)

I would like to write a view that create a custom queryset with only one tuple which is constituted field by field by the maximum value present among all the tuples.

id a b c d
0 2 4 1 7
1 3 1 6 3
2 8 4 2 1

The view should return 1 tuple with a=8, b=4, c=6 and d=7

How can I do that? Thanks

1 Answers

You can achieve this using aggregating a Max value for your columns:

from django.db.models import Max

myTable.objects.aggregate(a=Max("a"), b=Max("b"), C=Max("c"), d=Max("d"))
Related