基于另一个 Object 的值的总计 Object
Total Objects based on Value of Another Object
我正在使用 Django 1.8.3 和 Python 3.4.3
我会尽力解释这一点。如果这个 post 有更好的标题,请提出建议。
我们从电子邮件和网站流量来源将数据提取到我们的应用程序中,然后在视图模板中以 table 格式显示数据。我正在添加一个 table 来显示总数,这将帮助我们更好地可视化我们的电子邮件活动。我想呈现的一件事是当前数据代表的总竞选月数。例如:如果我们跟踪从 1 月到 6 月的数据,则我们的广告系列总月数将为 6。
我是 Python 的新手,所以很难找到一个解决方案,特别是因为我需要计算一个字符串数组(月)并提供和 int return(6 - 与上面的例子)。
下面是我的模型class,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 文件的一个片段,展示了我们如何每月获取收件人的当前计数。
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)
...
感谢您的宝贵时间。
如果我正确理解了你打算做什么以及你的模型是如何工作的,你可以这样做:
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,则该月收件人的总和不为零。
这个查询应该可以完成工作:
count = Email.objects.filter(recipients__gt=0).values('month').distinct().count()
我正在使用 Django 1.8.3 和 Python 3.4.3
我会尽力解释这一点。如果这个 post 有更好的标题,请提出建议。
我们从电子邮件和网站流量来源将数据提取到我们的应用程序中,然后在视图模板中以 table 格式显示数据。我正在添加一个 table 来显示总数,这将帮助我们更好地可视化我们的电子邮件活动。我想呈现的一件事是当前数据代表的总竞选月数。例如:如果我们跟踪从 1 月到 6 月的数据,则我们的广告系列总月数将为 6。
我是 Python 的新手,所以很难找到一个解决方案,特别是因为我需要计算一个字符串数组(月)并提供和 int return(6 - 与上面的例子)。
下面是我的模型class,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 文件的一个片段,展示了我们如何每月获取收件人的当前计数。
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)
...
感谢您的宝贵时间。
如果我正确理解了你打算做什么以及你的模型是如何工作的,你可以这样做:
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,则该月收件人的总和不为零。
这个查询应该可以完成工作:
count = Email.objects.filter(recipients__gt=0).values('month').distinct().count()