Django: Keeping the latest row for rows that have the same value in a column

Viewed 21

So right now I have a table that is similar to:

ID NAME Date
1 BOB 2007
2 BOB 2007
3 BOB 2008

and I want to remove the first two and keep the third row. I've tried using distinct() with order_by() with something like:

output = table.objects.all().order_by('NAME', '-Date').distinct('NAME')

but it only seems to remove one one of the first two rows. I am not using PostgreSQL unfortunately which is why I think the code above doesn't work as intended.

0 Answers
Related