1

Say you have the following model:

class Student(models.Model):
    student_no = models.IntegerField(primary_key=True)
    GPA = models.DecimalField(max_digits=65535, decimal_places=65535)
    year_of = days_on_market = models.IntegerField()

and you want to associate the highest GPA with ALL students. How do you go about doing that? I have tried the following:

Student.objects.annotate(max_GPA = Max('GPA')).aggregate(Max('GPA'))

As per the documentation. But, this does not associate the max GPA with each student. It only associate the max GPA for a record which is the same as the GPA of that record with the record, which is as useless as it gets. It also returns a dictionary {'GPA__max': 4.3}, but that is not the intention of the code.

I have looked here, but, this solution does the same thing as the other one, namely associating the max GPA of the record (and not max GPA of all records), which is the same as the GPA anyway.

In effect, I want that 4.3 to be associated with every student.

Any creative solution?

Community
  • 1
  • 1
EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
  • Well, it seems odd that you would want this... seeming as the maximum gpa has nothing to do with every other student... would it be possible just to have an aggregate query and pass the result to whatever needs it? – Will S Mar 02 '16 at 17:13

1 Answers1

-1

Try removing the aggregate part:

students = Student.objects.annotate(max_GPA = Max('GPA')).all()  # or filter

And the you should be able to access:

print students[0].max_GPA
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • Not working. The max_GPA will always be equal to the GPA for the record at hand. So, student[0].max_GPA != student[1].max_GPA. – EarlyCoder Mar 03 '16 at 15:47
  • If every Student has only one GPA, what do you expect? please be more specific – Gocht Mar 03 '16 at 15:52