本文整理汇总了Python中django.contrib.postgres.fields.jsonb.KeyTextTransform方法的典型用法代码示例。如果您正苦于以下问题:Python jsonb.KeyTextTransform方法的具体用法?Python jsonb.KeyTextTransform怎么用?Python jsonb.KeyTextTransform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.postgres.fields.jsonb
的用法示例。
在下文中一共展示了jsonb.KeyTextTransform方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def filter(self, qs, value):
if value in EMPTY_VALUES:
return qs
for expr in value:
if expr in EMPTY_VALUES: # pragma: no cover
continue
lookup_expr = expr.get("lookup", self.lookup_expr)
# "contains" behaves differently on JSONFields as it does on TextFields.
# That's why we annotate the queryset with the value.
# Some discussion about it can be found here:
# https://code.djangoproject.com/ticket/26511
if isinstance(expr["value"], str):
qs = qs.annotate(
field_val=KeyTextTransform(expr["key"], self.field_name)
)
lookup = {f"field_val__{lookup_expr}": expr["value"]}
else:
lookup = {
f"{self.field_name}__{expr['key']}__{lookup_expr}": expr["value"]
}
qs = qs.filter(**lookup)
return qs
示例2: create_activity_upcoming_notifications
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def create_activity_upcoming_notifications():
# Oh oh, this is a bit complex. As notification.context is a JSONField, the participants_already_notified subquery
# would return a jsonb object by default (which can't be compared to integer).
# We can work around this by transforming the property value to text ("->>" lookup) and then casting to integer
with timer() as t:
participants_already_notified = Notification.objects.\
order_by().\
filter(type=NotificationType.ACTIVITY_UPCOMING.value).\
exclude(context__activity_participant=None).\
values_list(Cast(KeyTextTransform('activity_participant', 'context'), IntegerField()), flat=True)
activities_due_soon = Activity.objects.order_by().due_soon()
participants = ActivityParticipant.objects.\
filter(activity__in=activities_due_soon).\
exclude(id__in=participants_already_notified).\
distinct()
for participant in participants:
Notification.objects.create(
type=NotificationType.ACTIVITY_UPCOMING.value,
user=participant.user,
expires_at=participant.activity.date.start,
context={
'group': participant.activity.place.group.id,
'place': participant.activity.place.id,
'activity': participant.activity.id,
'activity_participant': participant.id,
},
)
stats_utils.periodic_task('notifications__create_activity_upcoming_notifications', seconds=t.elapsed_seconds)
示例3: __call__
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def __call__(self, *args, **kwargs):
return KeyTextTransform(self.key_name, *args, **kwargs)
示例4: gen_facility_header
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def gen_facility_header(facility):
return {
'en': 'facilities_{}'.format(facility),
'zh': '提供家具_{}?'.format(facility),
'annotate': KeyTextTransform(facility,'facilities'),
}
示例5: __init__
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def __init__(
self,
column,
zh,
en=None,
field=None,
enum=None,
annotate=None,
fn=None,
child_fields=[]):
self.column = column
self.zh = zh
self.en = en if en else column
self.field = field
self.annotate = annotate
self.fn = fn
self.child_fields = []
if field:
self.en = '{}_{}'.format(self.en, field)
if not annotate and self.field:
self.annotate = KeyTextTransform(self.field, self.column)
for child in child_fields:
self.child_fields.append(Field(**child))
示例6: value
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def value(self):
return self.annotate(
value=Cast(KeyTextTransform('value', 'form_data'), output_field=FloatField())
).aggregate(
Count('value'),
Avg('value'),
Sum('value'),
)
示例7: activity_modified
# 需要导入模块: from django.contrib.postgres.fields import jsonb [as 别名]
# 或者: from django.contrib.postgres.fields.jsonb import KeyTextTransform [as 别名]
def activity_modified(sender, instance, **kwargs):
activity = instance
# abort if activity was just created
if not activity.id:
return
old = Activity.objects.get(id=activity.id)
participants = activity.activityparticipant_set
def delete_notifications_for_participants(participants, type):
Notification.objects.order_by().not_expired() \
.filter(type=type) \
.annotate(participant_id=Cast(KeyTextTransform('activity_participant', 'context'), IntegerField())) \
.filter(participant_id__in=participants.values_list('id', flat=True)) \
.delete()
if old.is_disabled != activity.is_disabled:
if activity.is_disabled:
delete_notifications_for_participants(
participants=participants,
type=NotificationType.ACTIVITY_UPCOMING.value,
)
Notification.objects.create_for_activity_participants(
participants=participants.exclude(user=activity.last_changed_by),
type=NotificationType.ACTIVITY_DISABLED.value,
)
else:
# activity is enabled
Notification.objects.create_for_activity_participants(
participants=participants.exclude(user=activity.last_changed_by),
type=NotificationType.ACTIVITY_ENABLED.value,
)
# activity_upcoming notifications will automatically get created by cronjob
if abs((old.date.start - activity.date.start).total_seconds()) > 60:
Notification.objects.create_for_activity_participants(
participants=participants.exclude(user=activity.last_changed_by),
type=NotificationType.ACTIVITY_MOVED.value,
)
# Issue