所以,我正在尝试用我的数据创建一个数据透视表,但我一直认为'NoneType'对象是不可迭代的。我正在使用Chartit。我的错误特别是'terms':['tot_ft']}])这是在数据透视图部分
这是我的代码: 以下是我导入的以下内容:
from django.shortcuts import render, get_list_or_404, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from basketball.models import Stats, Player, League, Game, Team
from django.contrib.auth import get_user
from django.db.models import Sum
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail, BadHeaderError
from chartit import PivotDataPool, PivotChart
views.py
def player_pivot_chart_view(request):
# Step 1: Create a PivotDataPool with the data we want to retrieve
playerPivotData = PivotDataPool(
series =
[{'options': {
'source': Stats.objects.all(),
'categories': 'player__first_name'},
'terms': {
'tot_ft':Sum('freeThrowsMade')}}])
pivcht = PivotChart(
datasource = playerPivotData,
series_options = [
{'options': {
'type': 'column',
'stacking': True},
'terms': ['tot_ft']}])
return render(request,'basketball/StatsPage.html', {'dataChart':pivcht})
models.py
class Stats(models.Model):
player = models.ForeignKey(Player)
game = models.ForeignKey(Game)
freeThrowsMade = models.PositiveIntegerField(verbose_name='FTm', default = 0)
freeThrowsAttempted = models.PositiveIntegerField(verbose_name='FTa', default = 0)
twoPointsMade = models.PositiveIntegerField(verbose_name='2Pm', default = 0)
twoPointsAttempted = models.PositiveIntegerField(verbose_name='2Pa', default = 0)
threePointsMade = models.PositiveIntegerField(verbose_name='3Pm', default = 0)
threePointsAttempted = models.PositiveIntegerField(verbose_name='3Pa', default = 0)
blocks = models.PositiveIntegerField(default = 0)
steals = models.PositiveIntegerField(default = 0)
assists = models.PositiveIntegerField(default = 0)
fouls= models.PositiveIntegerField(default = 0)
答案 0 :(得分:3)
我得到了同样的错误。在图表源代码中进行了一点搜索之后,我找到了解决方案。
除了PivotChart
和data_source
series_options
之外,您可以向chart_options
构造函数提供可选参数。None
。如果没有给出chart_options,则默认情况下,chartit会为其分配chart_options
,从而导致出现错误。
这实际上是图表中的一个错误。
要绕过此操作,您应该将空字典发送为pivcht = PivotChart(
datasource = playerPivotData,
series_options = [
{'options': {
'type': 'column',
'stacking': True},
'terms': ['tot_ft']}],
chart_options = {})
:
{{1}}
答案 1 :(得分:1)
我遇到了同样的问题,我知道出错的方法是将数据发送到一个模板。我把图表中的数据放在我的“base.html上。每次打开一个不是接收数据的模板时,我都会收到错误。请确保你没有这样做。