Django - object is not subscriptable django charts.js" or nothing displays at all?

岁酱吖の 提交于 2020-07-23 06:31:10

问题


i'm trying to display data on my charts using some model values and charts.js. My values are a country field and a class method that i'm not sure how I'm to call it.

I keep getting the error

"TypeError: 'Organization' object is not subscriptable django charts.js" or nothing displays at all. Please help.

models.py

from django_countries.fields import CountryField

class Organization(models.Model):
    country = CountryField(blank_label='(select country)')

    def Total_Score(cls):
        Total_Score = cls.grants_score() + cls.internal_score() #This is a sum of other class methods. This part works as is.
        return Total_Score

Views.py

def scores_charts(request):

    labels = []
    data = []

    orgz = Organization.objects.values('country').annotate(Total_Scores=Organization.objects.values('Total_Scores()'))
    
    for org in orgz:
        labels.append(org['country'])
        labels.append(org['Total_Scores'])
    
    return JsonResponse(data={
        'labels': labels,
        'data': data,
    }) 

Template.html

<div class="card-block-big">
  <canvas id="scores_charts" data-url="{% url 'scores_charts' %}"></canvas> 
</div>

   <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
   <script src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3/dist/Chart.min.js"></script>

          <script>
        
            $(function () {
        
              var $scores_charts = $("#scores_charts");
              $.ajax({
                url: $scores_charts.data("url"),
                success: function (data) {
        
                  var ctx = $scores_charts[0].getContext("2d");
        
                  new Chart(ctx, {
                    type: 'bar',
                    data: {
                      labels: data.labels,
                      datasets: [{
                        label: 'Scores',
                        backgroundColor: 'blue',
                        data: data.data
                      }]          
                    },
                    options: {
                      responsive: true,
                      legend: {
                        position: 'top',
                      },
                      title: {
                        display: true,
                        text: 'Scores Bar Chart'
                      }
                    }
                  });
        
                }
              });
        
            });
        
          </script>

回答1:


This error

"TypeError: 'Organization' object is not subscriptable django charts.js"

suggests that you are trying to get an element out of an Organization object that isn't a dictionary, list, or a tuple.

I suspect the likely cause of this may be stemming from the following (though you can confirm this with the full error trace which should tell you the line of code that is causing the error):

for org in orgz:
  labels.append(org['country'])
  labels.append(org['Total_Scores'])

You should try to debug the values of this and verify that they are correct.



来源:https://stackoverflow.com/questions/62856954/django-object-is-not-subscriptable-django-charts-js-or-nothing-displays-at-al

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!