本文整理匯總了Python中django.db.models.functions.TruncYear方法的典型用法代碼示例。如果您正苦於以下問題:Python functions.TruncYear方法的具體用法?Python functions.TruncYear怎麽用?Python functions.TruncYear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models.functions
的用法示例。
在下文中一共展示了functions.TruncYear方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: stats
# 需要導入模塊: from django.db.models import functions [as 別名]
# 或者: from django.db.models.functions import TruncYear [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
示例2: get_context_data
# 需要導入模塊: from django.db.models import functions [as 別名]
# 或者: from django.db.models.functions import TruncYear [as 別名]
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
documents = Document.objects
if self.request.user.is_reviewer:
documents = documents.filter(
taskqueue__user_id=self.request.user.pk,
textunit__dateusage__isnull=False).distinct()
ctx['documents'] = documents.values('pk', 'name').iterator()
periods_qs = DateUsage.objects
# got rid of probably false-pos
periods_qs = periods_qs.filter(date__gte=datetime.date.today() - datetime.timedelta(365 * 300),
date__lte=datetime.date.today() + datetime.timedelta(365 * 100))
periods_qs = periods_qs \
.annotate(year=TruncYear('date')).values('year') \
.annotate(count=Count('date', distinct=True)).order_by('year')
periods = []
start = end = count = years_count = 0
limit = 1000
years_count_limit = 10
flag = False
current_year = datetime.date.today().year
# get periods to truncate data by periods otherwise d3 hangs on large datasets
for q in periods_qs:
if not start:
start = q['year']
if count + q['count'] > limit or years_count > years_count_limit:
periods.append({'start': start.year, 'end': end.year, 'count': count,
'checked': start.year <= current_year <= end.year})
count = q['count']
start = q['year']
years_count = 0
flag = True
else:
count += q['count']
years_count += 1
flag = False
end = q['year']
if not flag and count:
periods.append({'start': start.year, 'end': end.year, 'count': count,
'checked': start.year <= current_year <= end.year})
ctx['periods'] = periods
return ctx
示例3: museum_view
# 需要導入模塊: from django.db.models import functions [as 別名]
# 或者: from django.db.models.functions import TruncYear [as 別名]
def museum_view(request):
def convert_timedelta(td):
return {
'year': td.days // 365,
'day': td.days % 365,
'hour': td.seconds // 3600,
'minute': (td.seconds % 3600) // 60,
'second': td.seconds % 60
}
ctx = {}
ctx['total_problem_count'] = Problem.objects.count()
ctx['total_submission_count'] = Submission.objects.count()
ctx['total_user_count'] = User.objects.filter(is_active=True).count()
# NOTE: this will break if there is no submission at all
first_submission = Submission.objects.last()
ctx['first_submission_time'] = first_submission.create_time
ctx['first_submission_duration'] = convert_timedelta(datetime.now() - ctx['first_submission_time'])
ctx['first_submission_author'] = first_submission.author
from uptime import uptime
ctx['uptime'] = convert_timedelta(timedelta(seconds=uptime()))
ctx['server_time'] = datetime.now()
ctx['eoj3_create_duration'] = convert_timedelta(datetime.now() - datetime(2017, 3, 11, 18, 32))
ctx['submission_count_1'] = Submission.objects.filter(create_time__gt=datetime.now() - timedelta(days=1)).count()
ctx['submission_count_7'] = Submission.objects.filter(create_time__gt=datetime.now() - timedelta(days=7)).count()
ctx['submission_count_30'] = Submission.objects.filter(create_time__gt=datetime.now() - timedelta(days=30)).count()
ctx['submission_stat'] = Submission.objects.filter(create_time__gt=datetime.today() - timedelta(days=30)). \
annotate(date=TruncDate('create_time')).values('date'). \
annotate(count=Count('id')).values('date', 'count').order_by()
ctx['user_stat'] = User.objects.filter(is_active=True).annotate(date=TruncYear('date_joined')).values('date'). \
annotate(count=Count('id')).values('date', 'count').order_by("date")
for idx, user in enumerate(ctx['user_stat']):
if idx == 0:
continue
user['count'] += ctx['user_stat'][idx - 1]['count']
ctx['problem_stat'] = Problem.objects.annotate(date=TruncYear('create_time')).values('date'). \
annotate(count=Count('id')).values('date', 'count').order_by("date")
for idx, user in enumerate(ctx['problem_stat']):
if idx == 0:
continue
user['count'] += ctx['problem_stat'][idx - 1]['count']
ctx['servers'] = servers = Server.objects.filter(enabled=True)
for server in servers:
server.status = ping(server)
return render(request, 'museum.jinja2', context=ctx)