按关键字过滤数据库中的对象

时间:2015-07-23 20:33:19

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

我正在使用Django 1.8.3和Python 3.4.3

我会尝试解释这一点,所以它有道理,所以请耐心等待。我已经将csv文件导入到我的数据库中,其中包含大约50行和10列数据,其中包括从Google Analytics中提取的数据以及我们的电子邮件营销活动数据。其中一列是'day_of_week'。我需要计算具有我需要的那天关键字的数据库行...我可以弄清楚如何做到的唯一方法是使用下面的代码,但是男人,看来它可能更有动态更清洁。

有没有办法以我可以在模板中使用标签的方式过滤它,或者比这更清洁的东西?谢谢你的帮助。

class EmailListView(ListView):
    model = Email
    template_name = 'dashboard/pages/email.html'

    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 = ['FMG - Business', 'FMG - Residential', 'AE', 'IBA']

        total_campaigns_monday = {}
        total_campaigns_tuesday = {},
        total_campaigns_wednesday = {},
        total_campaigns_thursday = {},
        total_campaigns_friday = {},
        total_campaigns_saturday = {},
        total_campaigns_sunday = {},
        total_recipients = {}
        total_unsubscribes = {}
        total_bounces = {}
        total_open = {}
        total_clicks = {}

        for campaigns in days:
            total_campaigns_monday = Email.objects.filter(day_of_week='Monday').count()
            total_campaigns_monday = Email.objects.filter(day_of_week='Tuesday').count()
            total_campaigns_monday = Email.objects.filter(day_of_week='Wednesday').count()
            total_campaigns_monday = Email.objects.filter(day_of_week='Thursday').count()
            total_campaigns_monday = Email.objects.filter(day_of_week='Friday').count()
            total_campaigns_monday = Email.objects.filter(day_of_week='Saturday').count()
            total_campaigns_monday = Email.objects.filter(day_of_week='Sunday').count()

我的模板片段(注意第一个与其他模板不同。)

{% 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>
{% endif %}

3 个答案:

答案 0 :(得分:2)

我希望我理解你的问题。

替换total_campaigns_<day_of_week>语句和for循环

total_campaigns = {}
for day in days:
    total_campaigns[day] = Email.objects.filter(day_of_week=day).count()

在模板中,您可以使用{{ total_campaigns.Monday }}{{ total_campaigns.Sunday }}或类似的内容。

答案 1 :(得分:1)

您可以使用aggregation methods在一个查询中执行此操作:

from django.db.models import Count
days_and_counts = Email.objects.values('day_of_week').annotate(count=Count('day_of_week'), distinct=True)

会在days_and_counts(从我随机填充的数据集中输出)中给出类似的内容:

[
    {'day_of_week': 'Friday', 'count': 7},
    {'day_of_week': 'Monday', 'count': 5},
    {'day_of_week': 'Saturday', 'count': 2},
    {'day_of_week': 'Sunday', 'count': 12},
    {'day_of_week': 'Thursday', 'count': 11},
    {'day_of_week': 'Tuesday', 'count': 6},
    {'day_of_week': 'Wednesday', 'count': 7}
]

从那里,你可以建立像f43d65建议的字典:

total_campaigns = {x['day_of_week']: x['count'] for x in days_and_counts}

会在total_campaigns中为您提供类似的内容:

{
    'Wednesday': 7,
    'Thursday': 11,
    'Tuesday': 6,
    'Monday': 5,
    'Saturday': 2,
    'Sunday': 12,
    'Friday': 7
}

答案 2 :(得分:1)

是的,你可以更清洁:

        days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

        total_campaigns = {
            day:Email.objects.filter(day_of_week=day).count()
            for day in days
        }

我建议查看词典理解