Search code examples
djangopivot-tablepivot-chart

Django-Chartit2 'dict_keys' object does not support indexing error


I am exploring Django Chartit2 package for charting purpose by following the demo examples provided here. I have created my own Model for Pivot Charts which contains fields: city, person and sales.

In my views I am creating PivotChart by making use of term which is based on aggregation of Sales column.

While trying accessing the url I am getting the error:

Exception Type: TypeError at /chart/pivotpool/

Exception Value: 'dict_keys' object does not support indexing

Let me know what I am missing while specifying the terms for PivotChart.

class SalesHistory(models.Model):
    city = models.CharField(max_length=50)
    person = models.CharField(max_length=50)
    sales_qty = models.IntegerField()

def some_view(request):
    ds = PivotDataPool(series=[{'options': {'source': SalesHistory.objects.all(), 'categories': ['city']},
    'terms': {'total_sales': Sum('sales_qty')}}])
    pivchrt = PivotChart(datasource=ds, series_options=[{'options': {'type': 'column', 'stacking': True}, 'terms':['total_sales']}], chart_options = {})

My traceback:

Traceback:
File "/home/sam/Documents/onemoreEnv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/sam/Documents/onemoreEnv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/sam/Documents/Lab Projects/djangoChartit/demoChartit/views.py" in some_view
  131.                        'text': 'city'}}})

File "/home/sam/Documents/onemoreEnv/lib/python3.4/site-packages/chartit/charts.py" in __init__
  508.         self.set_default_hcoptions()

File "/home/sam/Documents/onemoreEnv/lib/python3.4/site-packages/chartit/charts.py" in set_default_hcoptions
  533.         categories = dss[terms[0]]['categories']

Solution

  • The terms which is being used in the set_default_hcoptions function in charts.py file of chartit has no attribute index, thats the reason it is giving error to me. I have commented the line where it gets initialised i.e.

    # terms = self.series_options.keys()
    

    and have added my own version of it:

    terms = list(self.series_options)
    

    and it worked for me.