因此,我试图用我的数据创建数据透视图,但是我一直在获取'NoneType'对象是不可迭代的。我正在使用Chartit。我的错误专门与'terms':['tot_ft']}])一起出现在PivotChart部分中

这是我的代码:
这是我导入的以下内容:

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)

最佳答案

我遇到了同样的错误。在搜索Chartit源代码后,我找到了解决方案。

除了PivotChartdata_sourceseries_options之外,您还可以给chart_options构造函数提供一个可选参数。如果未提供chart_options,则默认情况下chartit将None分配给它,从而导致出现错误。
这实际上是Chartit中的错误。

要绕过它,您应该发送一个空字典作为chart_options

pivcht = PivotChart(
            datasource = playerPivotData,
            series_options = [
                {'options': {
                    'type': 'column',
                    'stacking': True},
                'terms': ['tot_ft']}],
            chart_options = {})

10-07 13:43