Django queryset:使用.distinct()和.count()处理表

问题描述 投票:0回答:1

我想处理一些Django表,以便在我的HTML模板中创建统计表。

例如,我需要从我的数据库表中获取所有distinct object,显示the count of each distinct object,...

示例:

我有一个名为Download的表,如下所示:

#TABLE DOWNLOAD
 ____________________________________________
| email                | PUB (FK_DOCUMENT)   |
| __________________________________________ |
|[email protected]      | 1                   |   
| __________________________________________ |
|[email protected]      | 2                   | 
| __________________________________________ |
|[email protected]      | 4                   | 
| __________________________________________ |
|[email protected]      | 3                   | 
| __________________________________________ |
|[email protected]      | 2                   | 
| __________________________________________ |
|[email protected]      | 4                   | 
| __________________________________________ |
|[email protected]      | 4                   | 
| __________________________________________ |

根据名为Document的表,该表有一个ForeignKey:

#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
  • qazxsw poi对应于表Requests中每个qazxsw poi事件的总和

这是我对PUB_ID的查询集:

Download

它返回:

PUB_ID

我的pub_id = Download.objects.values_list('pub__edqm_id').distinct() 查询集:

<QuerySet [('A',), ('B',), ('C',), ('D',)]>

它返回:

Requests

题 :

我如何填充我的HTML表格:

requests = Download.objects.values_list('pub__edqm_id').annotate(count=Count('pub__edqm_id'))

它显示在我的表格中:

<QuerySet [('A', 1), ('B', 2), ('C', 1), ('D', 3)]>

这是我的观点:

<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>
python django count distinct
1个回答
1
投票

这样的事情怎么样:

('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)
© www.soinside.com 2019 - 2024. All rights reserved.