1👍
Right, I think I have an answer for you that works – you would need to use Subquery and OuterRef. The first queryset orders all of the Obj models by the date that you want but filtered with OuterRef – the OuterRef gets the type field from the second line.
The second line then does the annotation with a subquery of the first line, the .values('date')[:1]
gets the last date value (which you may want to change around). The annotation means that each object is labelled with the date that the newest object with that type was created on.
from django.db.models import OuterRef, Subquery
dates = Obj.objects.filter(type=OuterRef('type')).order_by('-date')
Obj.objects.annotate(
newest_creation=Subquery(dates.values('date')[:1])
).order_by('date', 'newest_creation')
You might need to change the ordering of the date and newest_creation to get your desired ordering.
Source:stackexchange.com