Python django.db.models.expressions.CombinedExpression() Examples
The following are 4
code examples of django.db.models.expressions.CombinedExpression().
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.expressions
, or try the search function
.
Example #1
Source File: watcher.py From django_audit_trail with Apache License 2.0 | 6 votes |
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
Example #2
Source File: stats.py From online-judge with GNU Affero General Public License v3.0 | 5 votes |
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)))
Example #3
Source File: ordering.py From caluma with GNU General Public License v3.0 | 5 votes |
def get_ordering_value( self, qs: QuerySet, value: Any ) -> Tuple[QuerySet, OrderingFieldType]: return qs, CombinedExpression(F(self.field_name), "->", Value(value))
Example #4
Source File: contests.py From online-judge with GNU Affero General Public License v3.0 | 4 votes |
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