I have the following data,
This query groups by topicid, and then in each group gets the max date, frequency of posts and counts the number of authors as contributors,
info_model = InfoModel.objects.values('topicid')
.annotate( max=Max('date'), freq=Count('postid'),
contributors=Count('author', distinct=True))
This query can then be displayed as follows,
Q.1 (SOLVED) How can I order the rows by date, from most recent down? I did appended .order_by('date') to the query, which seems like the most obvious solution but this produces,
completely changing the 'freq' and 'contributions'.
EDIT: The ordering can be achieved by appending .order_by('-max')
Q.2 How can I display the 'post' for that date? So the post column should display,
ya
see ya
ciao
yoyo
I thought the following should work with {{ item.post }}, but no such luck.
<table class='table table-striped table-hover'>
<thead>
<tr>
<th>freq</th>
<th>topicid</th>
<th>date</th>
<th>contributors</th>
<th>post</th>
</tr>
</thead>
<tbody>
{% for item in info %}
<tr>
<td>{{ item.freq }}</td>
<td>{{ item.topicid }}</td>
<td>{{ item.max }}</td>
<td>{{ item.contributors }}</td>
<td>{{ item.post }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Thanks,
Edit:
I can get the correct result with raw SQL but can't do it wth a Django query,
info_model = list(InfoModel.objects.raw('SELECT *,
max(date),
count(postid) AS freq,
count(DISTINCT author) AS contributors FROM
crudapp_infomodel GROUP BY topicid ORDER BY date DESC'))
I simplified and reposted this problem here Rewrite raw SQL as Django query