Django 查询集:使用 .distinct() 和 .count() 处理 table
Django queryset : Handle table with .distinct() and .count()
我想处理一些 Django
table,以便在我的 HTML 模板中创建一个统计信息 table。
例如,我需要从我的数据库 table 中获取所有 distinct object
,显示 the count of each distinct object
,...
示例:
我有一个名为 Download
的 table,看起来像这样:
#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 |
| __________________________________________ |
这个 table 根据 table 有一个名为 Document
的外键:
#TABLE DOCUMENT
__________________
| ID | EDQM_ID |
| ________________ |
|1 | A |
| ________________ |
|2 | B |
| ________________ |
|3 | C |
| ________________ |
|4 | D |
| ________________ |
我想像这样创建一个 HTML table :
#HTML STATISTICS TABLE
________________________
| PUB_ID | Requests |
| _____________________ |
|A | 1 |
| _____________________ |
|B | 2 |
| _____________________ |
|C | 1 |
| _____________________ |
|D | 3 |
| _____________________ |
其中:
PUB_ID
对应于 PUB_ID
来自 table Download
Requests
对应于 table Download
中每个 PUB_ID
事件的总和
这是我 PUB_ID
的查询集:
pub_id = Download.objects.values_list('pub__edqm_id').distinct()
它 returns :
<QuerySet [('A',), ('B',), ('C',), ('D',)]>
我的 Requests
查询集:
requests = Download.objects.values_list('pub__edqm_id').annotate(count=Count('pub__edqm_id'))
它returns :
<QuerySet [('A', 1), ('B', 2), ('C', 1), ('D', 3)]>
问题:
如何填充我的 HTML table :
<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>
它显示在我的 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)
像这样的事情怎么样:
downloads = Download.objects.values('pub__edqm_id').annotate(count=Count('pub__edqm_id'))
context = {'downloads': downloads, ....
并在模板中
{% for download in downloads %}
{{ download.pub__edqm_id }} {{ download.count }}
{% endfor %}
我想处理一些 Django
table,以便在我的 HTML 模板中创建一个统计信息 table。
例如,我需要从我的数据库 table 中获取所有 distinct object
,显示 the count of each distinct object
,...
示例:
我有一个名为 Download
的 table,看起来像这样:
#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 |
| __________________________________________ |
这个 table 根据 table 有一个名为 Document
的外键:
#TABLE DOCUMENT
__________________
| ID | EDQM_ID |
| ________________ |
|1 | A |
| ________________ |
|2 | B |
| ________________ |
|3 | C |
| ________________ |
|4 | D |
| ________________ |
我想像这样创建一个 HTML table :
#HTML STATISTICS TABLE
________________________
| PUB_ID | Requests |
| _____________________ |
|A | 1 |
| _____________________ |
|B | 2 |
| _____________________ |
|C | 1 |
| _____________________ |
|D | 3 |
| _____________________ |
其中:
PUB_ID
对应于PUB_ID
来自 tableDownload
Requests
对应于 tableDownload
中每个
PUB_ID
事件的总和
这是我 PUB_ID
的查询集:
pub_id = Download.objects.values_list('pub__edqm_id').distinct()
它 returns :
<QuerySet [('A',), ('B',), ('C',), ('D',)]>
我的 Requests
查询集:
requests = Download.objects.values_list('pub__edqm_id').annotate(count=Count('pub__edqm_id'))
它returns :
<QuerySet [('A', 1), ('B', 2), ('C', 1), ('D', 3)]>
问题:
如何填充我的 HTML table :
<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>
它显示在我的 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)
像这样的事情怎么样:
downloads = Download.objects.values('pub__edqm_id').annotate(count=Count('pub__edqm_id'))
context = {'downloads': downloads, ....
并在模板中
{% for download in downloads %}
{{ download.pub__edqm_id }} {{ download.count }}
{% endfor %}