【问题标题】:what is the correct way to filter data from db in monthly and yearly basis for analytics?每月和每年从数据库中过滤数据以进行分析的正确方法是什么?
【发布时间】:2022-01-23 03:39:18
【问题描述】:

我正在为我的客户制作一个分析部分,我需要在其中以图形形式显示数据。为此,我正在使用图表 js。现在我有一个模型来存储网站的访问者。现在我想按月和按年过滤数据,将其放入图表 js 中并以图形方式显示。 现在我正在做的是这个

monthly_visitor = VisitorCount.objects.all().filter(date_of_record__month = today.month).count()

yearly_visitor = VisitorCount.objects.all().filter(date_of_record__year = today.year).count()

在这里,我可以获得当前月份和年份的计数。但是每个月和年都做变量是不对的。

所以请建议我过滤数据的正确程序是什么,以便我可以进行分析。我以前从来没有这样做过,所以我没有正确的方法,请建议我。

图表 js 代码也是这样的,这也可能不是进行分析的正确方法

const vismonth = document.getElementById('visitorMonthly').getContext('2d');
const visitorMonthly = new Chart(vismonth, {
    type: 'bar',
    data: {
        labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        datasets: [{
            label: 'Visitors Monthly',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});

const visyear = document.getElementById('visitorYearly').getContext('2d');
const visitorYearly = new Chart(visyear, {
    type: 'bar',
    data: {
        labels: ['2022', '2023', '2024', '2025', '2026', '2027'],
        datasets: [{
            label: 'Visitors Yearly',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            y: {
                beginAtZero: true
            }
        }
    }
});

请告诉我该怎么做

【问题讨论】:

    标签: python django django-queryset


    【解决方案1】:

    你可以在 django 中使用annotate

    from django.db.models import Count
    
    monthly_visitor = VisitorCount.objects.all().values(
        'date_of_record__month'
        ).annotate(
            total_in_month=Count('id') # take visitor_id or whatever you want to count
            ).order_by()
    

    它会给你类似的查询集。

    <QuerySet [{'created_at__month': 1, 'total_in_month': 5458}, {'created_at__month': 2, 'total_in_month': 4555}]
    

    【讨论】:

    • 此处的 Count('id) 显示 Count 名称未定义。 yiu 是如何定义它来计算 id 的
    • @RitankarBhattacharjee 正如我所提到的,而不是 id 输入您想要计算的内容。
    • 是的先生,我明白了,但我得到的错误是它说“名称计数未定义”所以我问你如何定义变量计数
    • 哦,我的错,编辑代码
    • 非常感谢,我正在浏览文档,但他们也没有提到导入,非常感谢
    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    相关资源
    最近更新 更多