XXX,DJANGO ORM里确实有很深的水,需要慢慢理解。。
比如:
获取指定时间段的数据:
app.deployversion_set.filter(add_date__range=(date_start, date_end))
按月获取记录数:
app_month = DeployVersion.objects.extra(select={'month': connections[DeployVersion.objects.db].ops.date_trunc_sql('month', 'add_date')}).values('month').annotate(dcount=Count('add_date'))
class DeployVersionSAAppView(TemplateView): template_name = 'version/sa_deployversion_app.html' def get_context_data(self, **kwargs): context = super(DeployVersionSAAppView, self).get_context_data(**kwargs) app_dict = {} app_month_dict = {} appcount = App.objects.annotate(num_app=Count('deployversion')) for app in appcount: if self.request.GET.has_key('date_start') and self.request.GET.has_key('date_end') : date_start = self.request.GET['date_start'] date_end = self.request.GET['date_end'] context['days'] = date_start+'至'+date_end app_qryset = app.deployversion_set.filter(add_date__range=(date_start, date_end)) else: context['days'] = '所有时间' app_qryset = app.deployversion_set.all() if app_qryset.count(): app_key = str(app.name) app_dict[app_key] = app_qryset.count() app_month = DeployVersion.objects.extra(select={'month': connections[DeployVersion.objects.db].ops.date_trunc_sql('month', 'add_date')}).values('month').annotate(dcount=Count('add_date')) for app_count in app_month: app_count_item = str(app_count['month'])[:7] if app_month_dict.has_key(app_count_item): app_month_dict[app_count_item] += 1 else: app_month_dict[app_count_item] = 1 app_month_list = sorted(app_month_dict.items(), key=lambda d: d[0], reverse=False) month_categories = [] month_data = [] for item_temp in app_month_list: month_categories.append(item_temp[0]) month_data.append(item_temp[1]) categories = app_dict.keys() data = app_dict.values() context['now'] = timezone.now() context['current_page'] = "deployversion-list-sa-app" context['form'] = SAAppForm context['categories'] = categories context['data'] = data context['month_categories'] = month_categories context['month_data'] = month_data return context
哈哈,本周个人超计划搞定。