I have a raw query returning one row which is then shown 8 times in the template. I have debugged and there is only one line when this is executed in the shell and I do a simple for loop print output. So it seems to be something about the template tags?
The raw query which definitely only returns one row (when executed in shell, mysql direct, debugged in pycharm):
digital_clients = DigitalClient.objects.raw('''select
dig.id,
dig.client,
dig.digital_campaign_spend,
dig.job_page_impressions,
dig.event_page_impressions,
dig.profile_page_impressions,
dig.job_page_redirect_impressions,
dig.event_page_redirect_impressions,
dig.profile_page_redirect_impressions,
dig.applications,
dig.cost_per_application,
dig.cost_per_hire,
dig.hires,
dig.percentage_complete,
dig.offset_dotmailer_reach,
dig.offset_dotmailer_clicks,
sum(cam.client_link_clicks) as 'client_link_clicks',
sum(cam.reach) as 'reach'
from dashboard_digitalclient dig
left join dashboard_dotmailercampaign cam on dig.id = cam.client_id
group by dig.id
order by dig.client''')
Then of course in the view I'm passing it into the context for the template:
digital_clients = get_digital_client_summary()
context = {
'digital_clients': digital_clients,
'last_refreshed': last_refreshed
}
return render(request, 'dashboard/all_clients_summary.html', context)
Then I'm doing this in the template:
{% for client in digital_clients %}
<tr>
<td><a href="{% url 'single_client_dashboard' client.id %}">{{ client.client }}</a></td>
<td>{{ client.reach|default_if_none:0|add:client.offset_dotmailer_reach }}</td>
<td>{{ client.client_link_clicks|default_if_none:0|add:client.offset_dotmailer_clicks }}</td>
<td>{{ client.job_page_impressions }}</td>
<td>{{ client.event_page_impressions }}</td>
<td>{{ client.profile_page_impressions }}</td>
<td>{{ client.job_page_redirect_impressions }}</td>
<td>{{ client.event_page_redirect_impressions }}</td>
<td>{{ client.profile_page_redirect_impressions }}</td>
<td>£{{ client.digital_campaign_spend }}</td>
<td>{{ client.percentage_complete }}%</td>
<td>{{ client.applications }}</td>
<td>{{ client.hires }}</td>
<td>£{{ client.cost_per_application }}</td>
<td>£{{ client.cost_per_hire }}</td>
<td><a href="{% url 'all_clients_summary' %}?refresh=1&client={{ client.id }}">Client refresh</a></td>
</tr>
{% endfor %}
And I get 8 rows in my table?
Help would be much appreciated as I'm super stuck on this one...
All the best,
Tom