Django 查询中聚合和 SUM 中的重复值

Repeated values in Django Query on aggregate and SUM

我正在使用 PyGal 在前端呈现一些图表。我的 django-view [基于功能] 有点像这样:

def random_view(request):
    values_list = list()
    camera_dict = dict()
    bar_chart = pygal.Bar(spacing=60, explicit_size=True, width=2000,
                          height=800, pretty_print=True, margin=5, x_label_rotation=60, show_minor_x_labels=True)
    bar_chart.x_labels = ['8 AM', '9 AM', '10 AM', '11 AM', '12 Noon', '13 PM', '14 PM',
                          '15 PM', '16 PM', '17 PM', '18 PM', '19 PM', '20 PM', '21 PM', '22 PM', '23 PM']

    if request.method == 'GET':
        profile = Profile.objects.get(user_profile=request.user)
        store_qs = Store.objects.filter(brand_admin=profile)
        for store in store_qs:
            cam_qs = Camera.objects.filter(install_location=store)
            for cam in cam_qs:
                for x in range(10, 22):
                    value = PeopleCount.objects.filter(
                        timestamp__date='2017-09-06', timestamp__hour=x, camera=cam).aggregate(Sum('people_count_entry'))['people_count_entry__sum']  # noqa
                    values_list.append(value)
                bar_chart.add(str(cam), values_list)
        context = {'test': camera_dict, 'fun': bar_chart.render_data_uri()}

    return render(request, 'reports/report_daily.html', context)

问题是我为两个不同的相机对象获得了相同的值。


信息:

例如,如果 storetwo cameras,假设 cam1 and cam2。我为两个凸轮获得了相同的值,但事实并非如此。

我不知道我哪里出错了。帮助表示赞赏

提前致谢:)

问题是您在 "camera" 循环之外定义了 values_list。您正在做的是构建一个列表,其中包含来自所有商店的所有相机的值。要为每个摄像机构建一个列表,请在 "camera" 循环中实例化 values_list

#...
for cam in cam_qs:
    values_list = []
    # ...