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