Hi,
I’m looking to create a table with data over different time periods. 1 day and 1 week.
My function works to return the data for the week, but I’d also like to combine the same function to show me the data for 1 single day.
I’ve tried creating different functions 1 for weekly results and 1 for daily, but I can’t get this to display in a single table, which I’m thinking its because they are different query sets and looping through each one seems to populate all of the results of the week query set into each row.
My view:
def get_average_interactions_by_user():
# Get the date range for the previous week
today = get_london_time().date()
start_of_week = today - timedelta(days=today.weekday())
start_of_previous_week = start_of_week - timedelta(days=7)
end_of_previous_week = start_of_week - timedelta(days=1)
# Query the Usergrowth model for the required data, grouped by project
results = usergrowth_data = Usergrowth.objects.filter(
capture_date__gte=start_of_previous_week,
capture_date__lte=end_of_previous_week
).values('project__name').annotate(
total_interactions=Sum('interactions',distinct=True),
total_user_count=Sum('user_count', distinct=True)
)
My issue here I think is because I am filtering on the start/end dates which don’t include today’s data.
my table in my template is:
<tbody>
{% for project in project_data %}
{% load humanize %}
<tr>
<td class="table-text">{{ project.project}}</td>
<td class="table-text table-row-text ">{{ project.user_count | intcomma }}</td>
<td class="table-text">{{ project.interactions}}</td> # This is for 1 day
<td class="table-text">
{% for project2 in results %}
{% if project.project == project2.project__name %}
{{ project2.total_interactions }} # Im trying to get 7 days here
{% endif %}
{% endfor %}
</td>
<td class="table-text"></td>
<td class="table-text"></td>
<td class="table-text"></td>
</tr>
{% endfor %}
</tbody>
I’m trying to get:
project | user count | interactions (day) | interactions (Week)
project1 | 10 | 50 | 100
project2 | 500 | 82 | 1000
...
Hope that makes sense.
Tom.