Python django.db.models.functions.TruncMonth() Examples
The following are 4
code examples of django.db.models.functions.TruncMonth().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.db.models.functions
, or try the search function
.
Example #1
Source File: viewsets.py From contratospr-api with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: views.py From autocompeter with Mozilla Public License 2.0 | 6 votes |
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
Example #3
Source File: tasks.py From polemarch with GNU Affero General Public License v3.0 | 5 votes |
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
Example #4
Source File: v1.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 4 votes |
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)