简体   繁体   中英

count distinct value in django model field

Hi I have a django model which has two field 'date' and 'result'.

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

I need to count the value of result following way

date 2017-03-27 passed=3 and failed=2

One of the probable solution is following

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

I am using above command in the django chartit in order to draw pie chart to show total number of passed and failed value.

View:

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'},
              })

But it assign the count value both to "passed" variable.I need to show in two separate variable passed and failed. How can I separate the value?

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

给出两个注释,返回的QuerySet将具有两个dict,其中包括“ passed”和“ failed”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM