按关键字过滤数据库中的对象
Posted
技术标签:
【中文标题】按关键字过滤数据库中的对象【英文标题】:Filtering Objects in Database by Keyword 【发布时间】:2015-10-14 08:34:33 【问题描述】:我正在使用 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()
还有我的模板的 sn-p(注意第一个与其他模板不同。)
% 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 %
【问题讨论】:
【参考方案1】:希望我理解你的问题。
将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
或类似的东西。
【讨论】:
哇,这太简单了——干净而且和我的其他代码非常相似!!非常感谢!!【参考方案2】:您可以使用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
【讨论】:
不应该是 '.annotate(count=Count('day_of_week'), distinct=True)' 吗? 感谢您抽出宝贵时间提供帮助。我选择了 f43d65 的解决方案,因为它与我的其余代码非常匹配。再次感谢您。 感谢马克,您的编辑。很高兴为您提供帮助,Studio Rooster — 很高兴您找到了适合您的东西!【参考方案3】:是的,你可以更清洁:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
total_campaigns =
day:Email.objects.filter(day_of_week=day).count()
for day in days
我建议检查字典理解
【讨论】:
以上是关于按关键字过滤数据库中的对象的主要内容,如果未能解决你的问题,请参考以下文章