Django admin / Chart js错误-super()至少接受1个参数(给定0)

问题描述 投票:0回答:1

我正在覆盖django管理员(使用changelist_view)以在管理员视图中的数据上方显示条形图(使用chart.js)。使用此示例作为参考:https://findwork.dev/blog/adding-charts-to-django-admin/

获取错误:

TypeError at /admin/machines/machinescurrentdaythreadbreaks/
super() takes at least 1 argument (0 given)
Request Method: GET
Request URL:    http://localhost/admin/machines/machinescurrentdaythreadbreaks/
Django Version: 1.9
Exception Type: TypeError
Exception Value:    
super() takes at least 1 argument (0 given)
Exception Location: /home/epicar/EPIC-Django/EPIC_AR/machines/admin.py in changelist_view, line 557
Python Executable:  /usr/bin/python
Python Version: 2.7.9
Python Path:    
['/home/epicar/EPIC-Django/EPIC_AR',
'/home/epicar/EPIC-Django/venv/lib/python2.7/site-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
Server time:    Thu, 9 Jan 2020 17:50:04 +0000

admin.py对于模型如下所示:

def changelist_view(self, request, extra_context=None):
    # Aggregate breaks per head, per needle
    chart_data = (
        MachinesCurrentDayThreadBreaks.objects.annotate(breaks=Count("sum"))
        .values("sum")
        .annotate(y=Count("sum"))
        .order_by("head_position")
    )

    # Serialize and attach the chart data to the template context
    as_json = json.dumps(list(chart_data), cls=DjangoJSONEncoder)
    extra_context = extra_context or {"chart_data": as_json}

    # Call the superclass changelist_view to render the page
    return super().changelist_view(request, extra_context=extra_context)
python django chart.js
1个回答
0
投票

问题是您使用Python Version: 2.7.9,而作者是艺术作品Python 3.7.3

您可以尝试使用它进行修复

return super(admin.ModelAdmin, self).changelist_view(request, extra_context=extra_context)

如果您的EmailSubscriberAdmin类看起来像文章中的那样

class EmailSubscriberAdmin(admin.ModelAdmin):
© www.soinside.com 2019 - 2024. All rights reserved.