Я хотел бы обработать некоторые Django
таблицы, чтобы создать статистическую таблицу в моем HTML-шаблоне.
Например, мне нужно получить все distinct object
из таблицы базы данных, отобразить the count of each distinct object
, ...
Пример:
У меня есть таблица с именем Download
, которая выглядит так:
#TABLE DOWNLOAD
____________________________________________
| email | PUB (FK_DOCUMENT) |
| __________________________________________ |
|test12@gmail.com | 1 |
| __________________________________________ |
|test12@gmail.com | 2 |
| __________________________________________ |
|test45@gmail.com | 4 |
| __________________________________________ |
|test22@gmail.com | 3 |
| __________________________________________ |
|test01@gmail.com | 2 |
| __________________________________________ |
|test98@gmail.com | 4 |
| __________________________________________ |
|test74@gmail.com | 4 |
| __________________________________________ |
Эта таблица имеет ForeignKey в соответствии с таблицей с именем Document
:
#TABLE DOCUMENT
__________________
| ID | EDQM_ID |
| ________________ |
|1 | A |
| ________________ |
|2 | B |
| ________________ |
|3 | C |
| ________________ |
|4 | D |
| ________________ |
Я хотел бы создать таблицу HTML, например:
#HTML STATISTICS TABLE
________________________
| PUB_ID | Requests |
| _____________________ |
|A | 1 |
| _____________________ |
|B | 2 |
| _____________________ |
|C | 1 |
| _____________________ |
|D | 3 |
| _____________________ |
Где:
PUB_ID
соответствует отдельному PUB_ID
из таблицы Download
Requests
соответствует сумме каждого PUB_ID
вхождения из таблицы Download
Это мой набор запросов для PUB_ID
:
pub_id = Download.objects.values_list('pub__edqm_id').distinct()
Возвращает:
<QuerySet [('A',), ('B',), ('C',), ('D',)]>
И мой набор запросов для Requests
:
requests = Download.objects.values_list('pub__edqm_id').annotate(count=Count('pub__edqm_id'))
Возвращает:
<QuerySet [('A', 1), ('B', 2), ('C', 1), ('D', 3)]>
Вопрос:
Как я могу заполнить свою таблицу HTML:
<table id="DocumentTable" class="table table-bordered table-striped table-condensed table_model">
<thead>
<caption style="border: inherit; background-color: lightgrey;">
<span><strong>Download per Publication</strong></span>
</caption>
<tr>
<th>{% trans 'Publications' %}</th>
<th>{% trans 'Requests' %}</th>
<th>{% trans 'Max download number' %}</th>
<th>{% trans 'Average download number' %}</th>
</tr>
</thead>
<tbody>
{% for item in pub_id %}
<tr>
<td>{{ item }}</td>
<td><span class="badge alert-danger"> Requests here </span></td>
<td> </td>
<td> </td>
</tr>
{% endfor %}
</tbody>
</table>
Отображается в моей таблице:
('A',)
('B',)
...
И это мое мнение:
class StatsView(View):
template_name = 'freepub/stats.html'
def get(self, request):
subtitle = _("Statistics")
#Some values display in my template
customers_count = Customer.objects.all().count()
publications_count = Publication.objects.all().count()
downloads_count = Download.objects.all().count()
last_download_temp = Download.objects.latest('id').download_date
last_download = last_download_temp.strftime('%Y-%m-%d %H:%M:%S ')
document = Document.objects.all()
#Populate HTML table
pub_id = Download.objects.values_list('pub__edqm_id').distinct()
fieldname = Download.objects.values_list('pub__edqm_id').annotate(count=Count('pub__edqm_id'))
context = {'subtitle': subtitle,
'fieldname': fieldname,
'customers_count': customers_count,
'publications_count': publications_count,
'downloads_count': downloads_count,
'last_download': last_download,
'document': document}
return render(request, self.template_name, context)