[Django]-Python/Django: Selecting max value of one-to-many relation and displaying it in HTML/View

2👍

Also you can try to use annotate

from django.db.models import Max

Plan.objects.values('plan_crisisID__crisis_ID').annotate(max_pk=Max('plan_ID'))

0👍

After some trying, I did something like this:

for crisis in crisisList:
    plansInCrisis = planList.filter(plan_crisisID__crisis_ID=crisis.crisis_ID)
    max = plansInCrisis[0]
    for plan in plansInCrisis:
        if(plan.plan_ID > max.plan_ID ): max = plan
    toDisplay.append(max)

As i’m new to python programming, I assume this is bad practice if I loop twice without using any inbuilt methods, but it works for now. Sorry for the poor question, cheers

Leave a comment