本文整理匯總了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)