本文整理汇总了Python中django.db.models.functions.TruncMonth方法的典型用法代码示例。如果您正苦于以下问题:Python functions.TruncMonth方法的具体用法?Python functions.TruncMonth怎么用?Python functions.TruncMonth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.functions
的用法示例。
在下文中一共展示了functions.TruncMonth方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spending_over_time
# 需要导入模块: from django.db.models import functions [as 别名]
# 或者: from django.db.models.functions import TruncMonth [as 别名]
def spending_over_time(self, request):
fiscal_year = request.query_params.get("fiscal_year")
queryset = self.filter_queryset(self.get_queryset())
if fiscal_year:
start_date, end_date = get_fiscal_year_range(int(fiscal_year))
queryset = queryset.filter(
date_of_grant__gte=start_date, date_of_grant__lte=end_date
)
queryset = (
queryset.without_amendments()
.annotate(month=TruncMonth("date_of_grant"))
.values("month")
.annotate(total=Sum("amount_to_pay"), count=Count("id"))
.values("month", "total", "count")
.order_by("month")
)
return Response(queryset)
示例2: stats_by_domain
# 需要导入模块: from django.db.models import functions [as 别名]
# 或者: from django.db.models.functions import TruncMonth [as 别名]
def stats_by_domain(domain):
searches = (
Search.objects
.annotate(month=TruncMonth('created'))
.values('month')
.annotate(count=Count('id'))
.values('month', 'count')
)
fetches = {}
for s in searches:
year = str(s['month'].year)
month = str(s['month'].month)
if year not in fetches:
fetches[year] = {}
fetches[year][month] = s['count']
search = TitleDoc.search()
search = search.filter('term', domain=domain.name)
documents = search.execute().hits.total
return fetches, documents
示例3: stats
# 需要导入模块: from django.db.models import functions [as 别名]
# 或者: from django.db.models.functions import TruncMonth [as 别名]
def stats(self, last: int) -> OrderedDict:
qs = self.filter(start_time__gte=now()-timedelta(days=last))
qs = qs.annotate(
day=dbfunc.TruncDay('start_time'),
month=dbfunc.TruncMonth('start_time'),
year=dbfunc.TruncYear('start_time'),
)
result = OrderedDict()
result['day'] = self._get_history_stats_by(qs, 'day')
result['month'] = self._get_history_stats_by(qs, 'month')
result['year'] = self._get_history_stats_by(qs, 'year')
return result
示例4: get
# 需要导入模块: from django.db.models import functions [as 别名]
# 或者: from django.db.models.functions import TruncMonth [as 别名]
def get(self, request, *args, **kwargs):
per_month = json.loads(self.request.GET.get('per_month', 'false'))
qs = self.get_queryset()
if per_month:
qs = qs.order_by('date') \
.annotate(start=TruncMonth('date')) \
.values('start') \
.annotate(count=Sum('count')).order_by()
data = list(qs)
visible_interval = 360
else:
qs = qs.order_by('date') \
.values(start=F('date')) \
.annotate(count=Sum('count'))
data = list(qs)
date_list_view = DateUsageListAPIView(request=self.request, format_kwarg={})
date_data = date_list_view.list(request=self.request).data
visible_interval = 180
max_value = qs.aggregate(m=Max('count'))['m']
min_value = qs.aggregate(m=Min('count'))['m']
range_value = max_value - min_value
for item in data:
item['weight'] = (item['count'] - min_value) / range_value
if per_month:
item['url'] = '{}?month_search={}'.format(
reverse('extract:date-usage-list'), item['start'].isoformat())
item['content'] = '{}, {}: {}'.format(item['start'].strftime('%B'),
item['start'].year, item['count'])
else:
item['url'] = '{}?date_search={}'.format(
reverse('extract:date-usage-list'), item['start'].isoformat())
item['content'] = '{}: {}'.format(item['start'].isoformat(), item['count'])
item['date_data'] = [i for i in date_data if i['date'] == item['start']]
initial_start_date = datetime.date.today() - datetime.timedelta(days=visible_interval)
initial_end_date = datetime.date.today() + datetime.timedelta(days=visible_interval)
ret = {'data': data,
'per_month': per_month,
'initial_start_date': initial_start_date,
'initial_end_date': initial_end_date}
return JsonResponse(ret)