代码之家  ›  专栏  ›  技术社区  ›  Essex

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

  •  0
  • Essex  · 技术社区  · 7 年前

    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                   | 
    | __________________________________________ |
    

    根据名为 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 对应于distinct 发布ID 下载
    • Requests 发布ID 下载

    这是我的查询集 发布ID :

    pub_id = Download.objects.values_list('pub__edqm_id').distinct()
    

    它返回:

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

    请求 :

    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)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Essex    7 年前

    像这样的事情怎么样:

    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 %}
    
    推荐文章