當前位置: 首頁>>代碼示例>>Python>>正文


Python expressions.CombinedExpression方法代碼示例

本文整理匯總了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))) 
開發者ID:DMOJ,項目名稱:online-judge,代碼行數:7,代碼來源:stats.py

示例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 
開發者ID:TriplePoint-Software,項目名稱:django_audit_trail,代碼行數:17,代碼來源:watcher.py

示例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)) 
開發者ID:projectcaluma,項目名稱:caluma,代碼行數:7,代碼來源:ordering.py

示例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 
開發者ID:DMOJ,項目名稱:online-judge,代碼行數:61,代碼來源:contests.py


注:本文中的django.db.models.expressions.CombinedExpression方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。