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'
)
๐คIain Shelvington
0๐
Program.objects.filter(equipment_id__startswith=b_id_to_search).latest("saved_date")
This should do.
๐คChymdy
- [Answered ]-Custom Django 'delete' action not firing from admin list view
- [Answered ]-Is there a way to get my "Author" field prepopulating in my Django form?
Source:stackexchange.com