本文整理汇总了Python中django.db.models.expressions.CombinedExpression方法的典型用法代码示例。如果您正苦于以下问题:Python expressions.CombinedExpression方法的具体用法?Python expressions.CombinedExpression怎么用?Python expressions.CombinedExpression使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.expressions
的用法示例。
在下文中一共展示了expressions.CombinedExpression方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ac_rate
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import CombinedExpression [as 别名]
def ac_rate(request):
rate = CombinedExpression(ac_count / Count('submission'), '*', Value(100.0), output_field=FloatField())
data = Language.objects.annotate(total=Count('submission'), ac_rate=rate).filter(total__gt=0) \
.order_by('total').values_list('name', 'ac_rate')
return JsonResponse(get_bar_chart(list(data)))
示例2: serialize_object
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import CombinedExpression [as 别名]
def serialize_object(self, instance):
""" Returns stringified values for tracked fields. """
data = {}
for field in instance._meta.fields:
# Skip untracked fields
not_tracked_field = (self.fields is not None and field.name not in self.fields)
if not_tracked_field or field.name in self.excluded_fields:
continue
value = field.value_from_object(instance)
# http://stackoverflow.com/questions/33672920/django-db-models-f-combined-expression
if isinstance(value, CombinedExpression):
instance.refresh_from_db()
return self.serialize_object(instance)
data[field.name] = value
return data
示例3: get_ordering_value
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import CombinedExpression [as 别名]
def get_ordering_value(
self, qs: QuerySet, value: Any
) -> Tuple[QuerySet, OrderingFieldType]:
return qs, CombinedExpression(F(self.field_name), "->", Value(value))
示例4: get_context_data
# 需要导入模块: from django.db.models import expressions [as 别名]
# 或者: from django.db.models.expressions import CombinedExpression [as 别名]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if not (self.object.ended or self.can_edit):
raise Http404()
queryset = Submission.objects.filter(contest_object=self.object)
ac_count = Count(Case(When(result='AC', then=Value(1)), output_field=IntegerField()))
ac_rate = CombinedExpression(ac_count / Count('problem'), '*', Value(100.0), output_field=FloatField())
status_count_queryset = list(
queryset.values('problem__code', 'result').annotate(count=Count('result'))
.values_list('problem__code', 'result', 'count'),
)
labels, codes = [], []
contest_problems = self.object.contest_problems.order_by('order').values_list('problem__name', 'problem__code')
if contest_problems:
labels, codes = zip(*contest_problems)
num_problems = len(labels)
status_counts = [[] for i in range(num_problems)]
for problem_code, result, count in status_count_queryset:
if problem_code in codes:
status_counts[codes.index(problem_code)].append((result, count))
result_data = defaultdict(partial(list, [0] * num_problems))
for i in range(num_problems):
for category in _get_result_data(defaultdict(int, status_counts[i]))['categories']:
result_data[category['code']][i] = category['count']
stats = {
'problem_status_count': {
'labels': labels,
'datasets': [
{
'label': name,
'backgroundColor': settings.DMOJ_STATS_SUBMISSION_RESULT_COLORS[name],
'data': data,
}
for name, data in result_data.items()
],
},
'problem_ac_rate': get_bar_chart(
queryset.values('contest__problem__order', 'problem__name').annotate(ac_rate=ac_rate)
.order_by('contest__problem__order').values_list('problem__name', 'ac_rate'),
),
'language_count': get_pie_chart(
queryset.values('language__name').annotate(count=Count('language__name'))
.filter(count__gt=0).order_by('-count').values_list('language__name', 'count'),
),
'language_ac_rate': get_bar_chart(
queryset.values('language__name').annotate(ac_rate=ac_rate)
.filter(ac_rate__gt=0).values_list('language__name', 'ac_rate'),
),
}
context['stats'] = mark_safe(json.dumps(stats))
return context