[Answered ]-Django get queryset which is latest of other column value

1๐Ÿ‘

If you are using PostgreSQL you can pass arguments to queryset.distinct() that specify the field(s) to return distinct values for in your query. You need to use order_by correctly here, you should order by the field your are passing to distinct and then order by the second field so that the records that you want are first ('-saved_date' here so you get the latest for each equipment_id)

progall = Program.objects.filter(
    equipment_id__startswith=str(b_id_to_search)
).order_by=(
    'equipment_id',
    '-saved_date'
).distinct(
    'equipment_id'
)

0๐Ÿ‘

Program.objects.filter(equipment_id__startswith=b_id_to_search).latest("saved_date")

This should do.

๐Ÿ‘คChymdy

Leave a comment