1

所以,我试图用我的数据创建一个数据透视图,但我不断得到“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

视图.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})

模型.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)
4

2 回答 2

3

我得到了同样的错误。在 chartit 源代码中进行了一些搜索后,我找到了解决方案。

除了和:之外,您还可以为PivotChart构造函数提供一个可选参数。当没有给出chart_options 时,chartit默认分配给它,导致你得到的错误。这实际上是 chartit 中的一个错误。data_sourceseries_optionschart_optionsNone

要绕过这个,你应该发送一个空字典chart_options

pivcht = PivotChart(
            datasource = playerPivotData,
            series_options = [
                {'options': {
                    'type': 'column',
                    'stacking': True},
                'terms': ['tot_ft']}],
            chart_options = {})
于 2013-08-16T13:25:51.490 回答
1

我遇到了同样的问题,我知道出了什么问题的方法是将数据发送到一个模板。我将图表中的数据放在我的“base.html”上。每次我打开一个不是接收数据的模板时,我都会收到那个错误。确保你没有这样做。

于 2017-01-23T21:14:22.200 回答