【问题标题】:Total Objects based on Value of Another Object基于另一个对象的值的对象总数
【发布时间】:2015-10-22 09:11:24
【问题描述】:

我正在使用 Django 1.8.3 和 Python 3.4.3

我会尽力解释这一点。如果这篇文章有更好的标题,请提出建议。

我们从电子邮件和网站流量来源将数据提取到我们的应用程序中,然后在视图模板中以表格格式显示数据。我正在添加一个表格来显示总数,这将帮助我们更好地可视化我们的电子邮件活动。我想呈现的一件事是当前数据代表的总活动月数。例如:如果我们跟踪从 1 月到 6 月的数据,我们的总活动月数将为 6。

我是 Python 新手,因此很难找到一个解决方案,尤其是因为我需要计算一个字符串数组(月)并提供和 int 返回(6 - 上面的示例)。

下面是我的模型类models.py

class Email(models.Model):
    date = models.DateField()
    time = models.TimeField()
    month = models.CharField(max_length=255, null=True)
    subject = models.CharField(max_length=255)
    day_of_week = models.CharField(max_length=255)
    subject_type = models.CharField(max_length=255)
    content_type = models.CharField(max_length=255)
    email_list = models.CharField(max_length=255)
    recipients = models.PositiveIntegerField()
    unsubscribes = models.PositiveIntegerField()
    bounces = models.PositiveIntegerField()
    open = models.PositiveIntegerField()

所以,我想要完成的是,如果查看上述模型 - 如果 recipients 的总和不等于 0,那么 month 计数是多少,所以每个月收件人都不等于0,该月将在月份计数中加 1。

这是我的 views.py 文件的 sn-p,显示我们如何获取每月收件人的当前计数。

views.py

def get_context_data(self, **kwargs):
        context = super(EmailListView, self).get_context_data(**kwargs)
        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
                  'November', 'December']
        subject_type = ['Offer', 'Sell', 'Fear', 'Learn']
        content_type = ['Offer', 'Sell', 'Fear', 'Learn']
        email_list = ['FMGB', 'FMGR', 'AE', 'IBA']

        total_campaigns = {}
        total_recipients = {}
        total_unsubscribes = {}
        total_bounces = {}
        total_open = {}
        total_clicks = {}
        total_campaigns_content = {}
        total_recipients_content = {}
        total_unsubscribes_content = {}
        total_bounces_content = {}
        total_open_content = {}
        total_clicks_content = {}
        total_campaigns_subject = {}
        total_recipients_subject = {}
        total_unsubscribes_subject = {}
        total_bounces_subject = {}
        total_open_subject = {}
        total_clicks_subject = {}

for month in months:
            # total count
            total_campaigns[month] = Email.objects.filter(month=month).count()
            # recipients
            total_recipients[month] = Email.objects.filter(month=month).aggregate(
                Sum('recipients')).get('recipients__sum', 0.00)
            # unsubscribes
            total_unsubscribes[month] = Email.objects.filter(month=month).aggregate(
                Sum('unsubscribes')).get('unsubscribes__sum', 0.00)
            # bounces
            total_bounces[month] = Email.objects.filter(month=month).aggregate(Sum('bounces')).get(
                'bounces__sum', 0.00)
            # opens
            total_open[month] = Email.objects.filter(month=month).aggregate(
                Sum('open')).get('open__sum', 0.00)
            # clicks
            total_clicks[month] = Email.objects.filter(month=month).aggregate(
                Sum('clicks')).get('clicks__sum', 0.00)
             ...

感谢您的宝贵时间。

【问题讨论】:

    标签: django python-3.x django-models django-templates django-views


    【解决方案1】:

    这个查询应该可以完成工作:

    count = Email.objects.filter(recipients__gt=0).values('month').distinct().count()
    

    【讨论】:

      【解决方案2】:

      如果我正确理解了您打算做什么以及您的模型是如何工作的,您可以这样做:

      count = 0
      for month in months:
          temp = Email.objects.filter(month=month, recipients != 0)
          if temp:
              count = count + 1
      

      如果没有对象与过滤器匹配,则 temp 为 None,因此 if temp 仅在有至少 1 个收件人的电子邮件时运行,我猜你不能有负收件人,所以如果一封电子邮件有收件人 > 0 则该月收件人的总和不为零。

      【讨论】:

        猜你喜欢
        • 2022-06-10
        • 1970-01-01
        • 1970-01-01
        • 2020-06-27
        • 2021-11-23
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多