基于另一个对象的值的总对象

时间:2015-07-30 20:30:09

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

我正在使用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()

所以,我想要完成的是如果查看上面的模型 - 如果month的总和不等于0,则recipients计数是多少,因此对于每个月的收件人不等于0,那个月会增加1个月计数。

以下是我的views.py文件的片段,以显示我们如何按月获取收件人的当前计数。

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)
             ...

感谢您的时间。

2 个答案:

答案 0 :(得分:2)

此查询应该完成工作:

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

答案 1 :(得分:0)

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

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然后该月的收件人总和不为零。