本文整理汇总了Python中django.db.models.ExpressionWrapper方法的典型用法代码示例。如果您正苦于以下问题:Python models.ExpressionWrapper方法的具体用法?Python models.ExpressionWrapper怎么用?Python models.ExpressionWrapper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models
的用法示例。
在下文中一共展示了models.ExpressionWrapper方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_matching_omim
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def _get_matching_omim(query, projects):
"""Returns OMIM records that match the given query string"""
records = Omim.objects.filter(
Q(phenotype_mim_number__icontains=query) | Q(phenotype_description__icontains=query)
).filter(phenotype_mim_number__isnull=False).annotate(
description_start=ExpressionWrapper(Q(phenotype_description__istartswith=query), output_field=BooleanField()),
mim_number_start=ExpressionWrapper(Q(phenotype_mim_number__istartswith=query), output_field=BooleanField()),
).only('phenotype_mim_number', 'phenotype_description').order_by(
'-description_start', '-mim_number_start', 'phenotype_description').distinct()[:MAX_RESULTS_PER_CATEGORY]
result = []
for record in records:
result.append({
'key': record.phenotype_mim_number,
'title': record.phenotype_description,
'description': '({})'.format(record.phenotype_mim_number) if record.phenotype_mim_number else None,
})
return result
示例2: _get_matching_hpo_terms
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def _get_matching_hpo_terms(query, projects):
"""Returns OMIM records that match the given query string"""
records = HumanPhenotypeOntology.objects.filter(
Q(hpo_id__icontains=query) | Q(name__icontains=query)
).annotate(
name_start=ExpressionWrapper(Q(name__istartswith=query), output_field=BooleanField()),
hpo_id_start=ExpressionWrapper(Q(hpo_id__istartswith=query), output_field=BooleanField()),
).only('hpo_id', 'name', 'category_id').order_by(
'-name_start', '-hpo_id_start', 'name').distinct()[:MAX_RESULTS_PER_CATEGORY]
result = []
for record in records:
result.append({
'key': record.hpo_id,
'title': record.name,
'description': '({})'.format(record.hpo_id),
'category': record.category_id,
})
return result
示例3: punishments
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def punishments(request):
name = request.resolver_match.url_name
if "ban" in name:
mode = "ban"
elif "mute" in name:
mode = "mute"
elif "gag" in name:
mode = "gag"
Punishment.objects.annotate(completion=ExpressionWrapper(F('created_at') + F('length'),
output_field=DateTimeField()))\
.filter(completion__lte=timezone.now(),
resolved=False,
length__isnull=False).update(resolved=True)
servers = Server.objects.all()
return render(request, 'pages/punishments/general.pug', {'mode': mode,
'servers': servers})
示例4: annotate_expires_at
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def annotate_expires_at(self):
"""Add an ``expires_at`` field to the queryset results."""
return self.annotate(
expires_at=ExpressionWrapper(
(F('last_beat') + F('timeout')),
output_field=EXPIRES_COLUMN_TYPE
)
)
示例5: _populate_storage_daily_summary_table
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def _populate_storage_daily_summary_table(self):
"""Populate the daily summary table."""
included_fields = [
"usage_start",
"usage_end",
"namespace",
"report_period_id",
"pod",
"node",
"persistentvolumeclaim",
"persistentvolume",
"storageclass",
"cluster_id",
"cluster_alias",
]
annotations = {
"volume_labels": Coalesce(F("persistentvolume_labels"), F("persistentvolumeclaim_labels")),
"persistentvolumeclaim_capacity_gigabyte": ExpressionWrapper(
F("persistentvolumeclaim_capacity_bytes") * math.pow(2, -30), output_field=DecimalField()
),
"persistentvolumeclaim_capacity_gigabyte_months": ExpressionWrapper(
F("persistentvolumeclaim_capacity_byte_seconds") / 86400 * 30 * math.pow(2, -30),
output_field=DecimalField(),
),
"volume_request_storage_gigabyte_months": ExpressionWrapper(
F("volume_request_storage_byte_seconds") / 86400 * 30 * math.pow(2, -30), output_field=DecimalField()
),
"persistentvolumeclaim_usage_gigabyte_months": ExpressionWrapper(
F("persistentvolumeclaim_usage_byte_seconds") / 86400 * 30 * math.pow(2, -30),
output_field=DecimalField(),
),
"data_source": Value("Storage", output_field=CharField()),
}
entries = OCPStorageLineItemDaily.objects.values(*included_fields).annotate(**annotations)
for entry in entries:
summary = OCPUsageLineItemDailySummary(**entry)
summary.save()
示例6: filter_cone_search
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def filter_cone_search(self, queryset, name, value):
"""
Executes cone search by annotating each target with separation distance from either the specified RA/Dec or
the RA/Dec of the specified target. Formula is from Wikipedia: https://en.wikipedia.org/wiki/Angular_distance
The result is converted to radians.
Cone search is preceded by a square search to reduce the search radius before annotating the queryset, in
order to make the query faster.
"""
if name == 'cone_search':
ra, dec, radius = value.split(',')
elif name == 'target_cone_search':
target_name, radius = value.split(',')
targets = Target.objects.filter(
Q(name__icontains=target_name) | Q(aliases__name__icontains=target_name)
).distinct()
if len(targets) == 1:
ra = targets[0].ra
dec = targets[0].dec
else:
return queryset.filter(name=None)
ra = float(ra)
dec = float(dec)
double_radius = float(radius) * 2
queryset = queryset.filter(ra__gte=ra - double_radius, ra__lte=ra + double_radius,
dec__gte=dec - double_radius, dec__lte=dec + double_radius)
separation = ExpressionWrapper(
180 * ACos(
(Sin(radians(dec)) * Sin(Radians('dec'))) +
(Cos(radians(dec)) * Cos(Radians('dec')) * Cos(radians(ra) - Radians('ra')))
) / Pi(), FloatField()
)
return queryset.annotate(separation=separation).filter(separation__lte=radius)
# hide target grouping list if user not logged in
示例7: pearls
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def pearls(self):
return (self.exclude(nb_likes=0)
.annotate(
dislike_rate=ExpressionWrapper(
Cast(F('nb_dislikes'), FloatField()) / F('nb_likes'), output_field=FloatField())
)
.filter(nb_ratings__gte=PEARLS_MIN_RATINGS, nb_ratings__lte=PEARLS_MAX_RATINGS, dislike_rate__lte=PEARLS_MAX_DISLIKE_RATE)
.order_by('dislike_rate'))
示例8: hot_problems
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def hot_problems(duration, limit):
cache_key = 'hot_problems:%d:%d' % (duration.total_seconds(), limit)
qs = cache.get(cache_key)
if qs is None:
qs = Problem.get_public_problems() \
.filter(submission__date__gt=timezone.now() - duration, points__gt=3, points__lt=25)
qs0 = qs.annotate(k=Count('submission__user', distinct=True)).order_by('-k').values_list('k', flat=True)
if not qs0:
return []
# make this an aggregate
mx = float(qs0[0])
qs = qs.annotate(unique_user_count=Count('submission__user', distinct=True))
# fix braindamage in excluding CE
qs = qs.annotate(submission_volume=Count(Case(
When(submission__result='AC', then=1),
When(submission__result='WA', then=1),
When(submission__result='IR', then=1),
When(submission__result='RTE', then=1),
When(submission__result='TLE', then=1),
When(submission__result='OLE', then=1),
output_field=FloatField(),
)))
qs = qs.annotate(ac_volume=Count(Case(
When(submission__result='AC', then=1),
output_field=FloatField(),
)))
qs = qs.filter(unique_user_count__gt=max(mx / 3.0, 1))
qs = qs.annotate(ordering=ExpressionWrapper(
0.5 * F('points') * (0.4 * F('ac_volume') / F('submission_volume') + 0.6 * F('ac_rate')) +
100 * e ** (F('unique_user_count') / mx), output_field=FloatField(),
)).order_by('-ordering').defer('description')[:limit]
cache.set(cache_key, qs, 900)
return qs
示例9: test_pivot_aggregate
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def test_pivot_aggregate(self):
shirt_sales = ShirtSales.objects.all()
data = ExpressionWrapper(F('units') * F('price'), output_field=DecimalField())
pt = pivot(ShirtSales, 'store__region__name', 'shipped', data, Avg, default=0)
for row in pt:
region_name = row['store__region__name']
for dt in (key for key in row.keys() if key != 'store__region__name'):
spends = [ss.units * ss.price for ss in shirt_sales if force_text(ss.shipped) == force_text(dt) and ss.store.region.name == region_name]
avg = sum(spends) / len(spends) if spends else 0
self.assertAlmostEqual(row[dt], float(avg), places=4)
示例10: _get_hits_order
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def _get_hits_order(self, request, order):
try:
result = ESTopRules(request).get(count=Rule.objects.count(), order=order)
except ESError:
queryset = Rule.objects.order_by('sid')
queryset = queryset.annotate(hits=models.Value(0, output_field=models.IntegerField()))
queryset = queryset.annotate(hits=models.ExpressionWrapper(models.Value(0), output_field=models.IntegerField()))
return queryset.values_list('sid', 'hits')
result = map(lambda x: (x['key'], x['doc_count']), result)
return result
示例11: _populate_daily_summary_table
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def _populate_daily_summary_table(self):
"""Populate the daily summary table."""
OCPUsageLineItemDailySummary.objects.all().delete()
included_fields = [
"usage_start",
"usage_end",
"namespace",
"report_period_id",
"pod",
"node",
"cluster_id",
"cluster_alias",
"node_capacity_cpu_cores",
"pod_labels",
]
annotations = {
"pod_usage_cpu_core_hours": F("pod_usage_cpu_core_seconds") / 3600,
"pod_request_cpu_core_hours": Sum(
ExpressionWrapper(F("pod_request_cpu_core_seconds") / 3600, output_field=DecimalField())
),
"pod_limit_cpu_core_hours": Sum(
ExpressionWrapper(F("pod_limit_cpu_core_seconds") / 3600, output_field=DecimalField())
),
"pod_usage_memory_gigabyte_hours": Sum(
ExpressionWrapper(F("pod_usage_memory_byte_seconds") / 3600, output_field=DecimalField())
)
* math.pow(2, -30),
"pod_request_memory_gigabyte_hours": Sum(
ExpressionWrapper(F("pod_request_memory_byte_seconds") / 3600, output_field=DecimalField())
)
* math.pow(2, -30),
"pod_limit_memory_gigabyte_hours": ExpressionWrapper(
F("pod_limit_memory_byte_seconds") / 3600, output_field=DecimalField()
)
* math.pow(2, -30),
"node_capacity_cpu_core_hours": F("node_capacity_cpu_core_seconds") / 3600,
"node_capacity_memory_gigabytes": F("node_capacity_memory_bytes") * math.pow(2, -30),
"node_capacity_memory_gigabyte_hours": ExpressionWrapper(
F("node_capacity_memory_byte_seconds") / 3600, output_field=DecimalField()
)
* math.pow(2, -30),
"cluster_capacity_cpu_core_hours": F("cluster_capacity_cpu_core_seconds") / 3600,
"cluster_capacity_memory_gigabyte_hours": ExpressionWrapper(
F("cluster_capacity_memory_byte_seconds") / 3600, output_field=DecimalField()
)
* math.pow(2, -30),
"total_capacity_cpu_core_hours": F("cluster_capacity_cpu_core_seconds") / 3600 * 2,
"total_capacity_memory_gigabyte_hours": ExpressionWrapper(
F("cluster_capacity_memory_byte_seconds") / 3600 * 2, output_field=DecimalField()
)
* math.pow(2, -30),
"data_source": Value("Pod", output_field=CharField()),
}
entries = OCPUsageLineItemDaily.objects.values(*included_fields).annotate(**annotations)
for entry in entries:
summary = OCPUsageLineItemDailySummary(**entry)
summary.save()
示例12: _front_page
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def _front_page(paging_size=settings.PAGING_SIZE, page=0, add_filter={}, add_q=[], as_of=None, days_back=50):
# TODO: weighting https://medium.com/hacking-and-gonzo/how-hacker-news-ranking-algorithm-works-1d9b0cf2c08d
# (P-1) / (T+2)^G
if as_of is None:
now = timezone.now()
else:
now = as_of
if connection.vendor == 'postgresql':
now_value = Value(now, output_field=fields.DateTimeField())
submission_age_float = ExpressionWrapper( ( now_value - F('created_at')), output_field=fields.DurationField())
submission_age_hours = ExpressionWrapper(Extract(F('tf'), 'epoch') / 60 / 60 + 2.1 , output_field=fields.FloatField())
real_p = ExpressionWrapper(F('points') - 1, output_field=fields.FloatField())
formula = ExpressionWrapper( F('p') / ( Power(F('tfh'), F('g')) +0.001) , output_field=fields.FloatField())
return Story.objects.select_related('user')\
.filter(duplicate_of__isnull=True)\
.filter(points__gte=1) \
.filter(created_at__gte=now - datetime.timedelta(days=days_back)) \
.filter(created_at__lte=now) \
.filter(**add_filter) \
.annotate(tf=submission_age_float) \
.annotate(tfh=submission_age_hours) \
.annotate(p=real_p) \
.annotate(g=Value(1.8, output_field=fields.FloatField())) \
.annotate(formula=formula) \
.order_by('-formula')[(page*paging_size):(page+1)*(paging_size)]
elif connection.vendor == 'sqlite':
now_value = Value(now, output_field=fields.DateTimeField())
submission_age_float = ExpressionWrapper( ( now_value - F('created_at')), output_field=fields.FloatField())
submission_age_hours = ExpressionWrapper(F('tf') / 60 / 60 / 1000000 + 2.1 , output_field=fields.FloatField())
real_p = ExpressionWrapper(F('points') - 1, output_field=fields.FloatField())
formula = ExpressionWrapper( F('p') / ( Power(F('tfh'), F('g')) +0.001) , output_field=fields.FloatField())
return Story.objects.select_related('user')\
.filter(duplicate_of__isnull=True)\
.filter(points__gte=1) \
.filter(created_at__gte=now - datetime.timedelta(days=days_back)) \
.filter(created_at__lte=now) \
.filter(**add_filter) \
.annotate(tf=submission_age_float) \
.annotate(tfh=submission_age_hours) \
.annotate(p=real_p) \
.annotate(g=Value(1.8, output_field=fields.FloatField())) \
.annotate(formula=formula) \
.order_by('-formula')[(page*paging_size):(page+1)*(paging_size)]
else:
raise NotImplementedError("No frontpage magic for database engine %s implemented"%(connection.vendor))
示例13: overview
# 需要导入模块: from django.db import models [as 别名]
# 或者: from django.db.models import ExpressionWrapper [as 别名]
def overview(request, event_url_name):
event = get_object_or_404(Event, url_name=event_url_name)
# permission
if not event.is_admin(request.user):
return nopermission(request)
num_helpers = event.helper_set.count()
num_coordinators = event.all_coordinators.count()
num_vegetarians = event.helper_set.filter(vegetarian=True).count()
num_shift_slots = Shift.objects.filter(job__event=event).aggregate(
Sum('number'))['number__sum']
empty_slots_expr = ExpressionWrapper(F('number') - F('num_helpers'),
output_field=fields.IntegerField())
num_empty_shift_slots = Shift.objects.filter(job__event=event) \
.annotate(num_helpers=Count('helper')) \
.annotate(empty_slots=empty_slots_expr) \
.aggregate(Sum('empty_slots'))['empty_slots__sum']
total_duration = ExpressionWrapper((F('end') - F('begin')) * F('number'),
output_field=fields.DurationField())
try:
hours_total = Shift.objects.filter(job__event=event) \
.annotate(duration=total_duration) \
.aggregate(Sum('duration'))['duration__sum']
except (OperationalError, OverflowError):
hours_total = None
except Exception as e:
# handle psycopg2.DataError without importing psycopg2
# happens on overflow with postgresql
if 'DataError' in str(e.__class__):
hours_total = None
else:
raise e
# render
context = {'event': event,
'num_helpers': num_helpers,
'num_coordinators': num_coordinators,
'num_vegetarians': num_vegetarians,
'num_shift_slots': num_shift_slots,
'num_empty_shift_slots': num_empty_shift_slots,
'hours_total': hours_total}
return render(request, 'statistic/overview.html', context)