簡體   English   中英

在Django模型字段中計算不同的值

[英]count distinct value in django model field

嗨,我有一個Django模型,其中有兩個字段“日期”和“結果”。

+----------+-------------+--------------------------
|date                                     | result |  
+----------+-------------+--------------------------
|2017-03-27                               | passed | 
|2017-03-27                               | passed | 
|2017-03-27                               | passed | 
|2017-03-27                               | failed |
|2017-03-27                               | failed | 
|2017-03-26                               | passed |
|2017-03-26                               | failed |
+-----------+-------------+--------------------------

我需要按照以下方式計算結果的值

日期2017-03-27通過= 3失敗= 2

可能的解決方案之一是

Results.objects.filter(date=date.today()).values('result').annotate(passed=Count('result'))},

我在django chartit中使用以上命令,以繪制餅圖以顯示通過和失敗值的總數。

視圖:

def chart(request):
    resultdata = DataPool(
           series=
            [{'options': {
               'source': Results.objects.filter(date=date.today()).values('result').annotate(passed=Count('result'))},
              'terms':[
                'date',
                'passed']}
             ])
    cht = Chart(
            datasource = resultdata,
            series_options =
              [{'options':{
                  'type': 'pie','stacking': False,
                  },
                'terms':{
                  'date':[
                    'passed']
                  }}],
            chart_options =
              {'title': {
                   'text': 'Result of test cases'},
              })

但是它將計數值都分配給“通過”變量。我需要在兩個單獨的變量中顯示通過和失敗。 如何區分價值?

Results.objects.filter(date=date.today()).values('result').annotate(passed=Count('result'), failed=Count('result'))

給出兩個注釋,返回的QuerySet將具有兩個dict,其中包括“ passed”和“ failed”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM