I created this simple set of data to illustrate my point. It is a simple model with no further relations to any other model.
I need to group the data above by topicid, find the max date for each group and then get the author for that date.
info = TempModel.objects
.values('topic')
.annotate( max=Max('date'))
.order_by('-max')
Iterating through this in a template,
<table>
{% for item in info %}
<tr>
<td>{{ item.topicid }}</td>
<td>{{ item.max }}</td>
<td>{{ item.author }}</td>
</tr>
{% endfor %}
</table>
produces,
How do I display the 'author' column for each max date?
I can do this with a raw sql query like this,
info = list(TempModel.objects
.raw('SELECT *, max(date) AS max
FROM crudapp_tempmodel
GROUP BY topicid
ORDER BY date DESC'))
But I want to do it using a Django query.
The output I am looking for is,
If this is not possible with a Django query I would like to know.
Thanks,