【发布时间】:2015-10-15 11:00:03
【问题描述】:
我正在使用 Django 1.8.3 和 Python 3.4.3
我的应用程序开始变大,我有几个“for”循环来访问同一模型中的多个对象。虽然我是 Python 新手,但我猜想有一种更简化的方法来准备我的代码。请看下面的小sn-p。该模型是“day_of_week”,但我有几个对象的“for”循环。我还放了一个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_campaigns_month = {}
total_recipients = {}
total_unsubscribes = {}
total_bounces = {}
total_open = {}
total_clicks = {}
for day in days:
total_campaigns[day] = Email.objects.filter(day_of_week=day).count()
for recipients in days:
total_recipients[recipients] = Email.objects.filter(day_of_week=recipients).aggregate(
Sum('recipients')).get('recipients__sum', 0.00)
for unsubscribes in days:
total_unsubscribes[unsubscribes] = Email.objects.filter(day_of_week=unsubscribes).aggregate(
Sum('unsubscribes')).get('unsubscribes__sum', 0.00)
for bounces in days:
total_bounces[bounces] = Email.objects.filter(day_of_week=bounces).aggregate(Sum('bounces')).get(
'bounces__sum', 0.00)
for open in days:
total_open[open] = Email.objects.filter(day_of_week=open).aggregate(
Sum('open')).get('open__sum', 0.00)
for clicks in days:
total_clicks[clicks] = Email.objects.filter(day_of_week=clicks).aggregate(
Sum('clicks')).get('clicks__sum', 0.00)
模板片段... (email.html)
{% if email_list %}
<tr>
<td>Monday</td>
<td>{{ total_campaigns.Monday }}</td>
<td>{{ total_recipients.Monday }}</td>
<td>{{ total_unsubscribes.Monday }}</td>
<td>{{ total_bounces.Monday }}</td>
<td>{{ total_open.Monday }}</td>
<td>{{ total_clicks.Monday }}</td>
<td>{% average total_open.Monday total_recipients.Monday %}</td>
<td>{% average total_clicks.Monday total_open.Monday %}</td>
</tr>
...
【问题讨论】:
-
您能否创建一个名为“initialize”(或类似名称)的函数,该函数接收您尝试迭代的内容以及任何其他变量,以便将重复代码缩减为单个环形?那有意义吗?编辑:下面的第一个答案对我来说看起来不错。我的建议是需要对同一个方法进行大量调用,但参数不同。
-
感谢您的建议。在我开始实施解决方案之前,是否可以询问您是否可以为这个菜鸟提供一个使用不同参数的建议示例?谢谢。
-
当然我会简单地尝试一个样本,但我警告你我也是一个菜鸟:)。
标签: django python-3.x django-models django-templates django-views