I have a table like this
Event table
| id | status | date | order(FK) |
|---|---|---|---|
| 1 | Planned | 05-02-2015 | 1 |
| 2 | Delivered | 04-02-2015 | 2 |
| 3 | Packed | 03-02-2015 | 3 |
| 4 | Return | 06-02-2015 | 1 |
I want output like this
| id | status | date | order(FK) |
|---|---|---|---|
| 2 | Delivered | 04-02-2015 | 2 |
| 3 | Packed | 03-02-2015 | 3 |
| 4 | Return | 06-02-2015 | 1 |
I tried with
query = Event.objects.annotate(order_num=Max('date'))
but didn't get the expected result. How can I achieve this output?
Try using the following:
from django.db.models import Max
Event.objects.annotate(max_date=Max('order__event__date')) \
.filter(date=F('max_date'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With