agiliq / django-graphos

Django charting made *really* easy.
http://agiliq.com/demo/graphos
BSD 2-Clause "Simplified" License
441 stars 98 forks source link

Issue with GoogleCharts and counting unique values #123

Open PaulSec opened 7 years ago

PaulSec commented 7 years ago

Hi,

It seems that you don't handle the dictionaries properly in your code, check this simple code:

    isps = Ip.objects.all().values('isp').annotate(the_count=Count('isp'))
    data_source = ModelDataSource(isps, fields=['isp', 'the_count'])
    chart = BarChart(data_source, options={'title': "ISP Count"})

The error I get is:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/xxx/xxx/xxx/views.py", line 75, in stats
    data_source = ModelDataSource(isps, fields=['isp', 'the_count'])
  File "/usr/local/lib/python3.5/dist-packages/graphos/sources/model.py", line 36, in __init__
    self.data = self.create_data()
  File "/usr/local/lib/python3.5/dist-packages/graphos/sources/model.py", line 41, in create_data
    data.append(get_field_values(row, self.fields))
  File "/usr/local/lib/python3.5/dist-packages/graphos/sources/model.py", line 8, in get_field_values
    value = getattr(row, field)
AttributeError: 'dict' object has no attribute 'isp'
PaulSec commented 7 years ago

I solved the issue by replacing:

value = getattr(row, field)

to:

value = row[field]