本文整理匯總了Python中django.db.models.expressions.Value方法的典型用法代碼示例。如果您正苦於以下問題:Python expressions.Value方法的具體用法?Python expressions.Value怎麽用?Python expressions.Value使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.models.expressions
的用法示例。
在下文中一共展示了expressions.Value方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def __init__(self, expression, pos, length=None, **extra):
"""
expression: the name of a field, or an expression returning a string
pos: an integer > 0, or an expression returning an integer
length: an optional number of characters to return
"""
if not hasattr(pos, 'resolve_expression'):
if pos < 1:
raise ValueError("'pos' must be greater than 0")
pos = Value(pos)
expressions = [expression, pos]
if length is not None:
if not hasattr(length, 'resolve_expression'):
length = Value(length)
expressions.append(length)
super(Substr, self).__init__(*expressions, **extra)
示例2: resolve_expression
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def resolve_expression(
self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False
):
resolved = super().resolve_expression(
query, allow_joins, reuse, summarize, for_save
)
if self.config:
if not hasattr(self.config, "resolve_expression"):
resolved.config = Value(self.config).resolve_expression(
query, allow_joins, reuse, summarize, for_save
)
else:
resolved.config = self.config.resolve_expression(
query, allow_joins, reuse, summarize, for_save
)
return resolved
示例3: batch_process_rhs
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def batch_process_rhs(self, compiler, connection, rhs=None):
if rhs is None:
rhs = self.rhs
if self.bilateral_transforms:
sqls, sqls_params = [], []
for p in rhs:
value = Value(p, output_field=self.lhs.output_field)
value = self.apply_bilateral_transforms(value)
value = value.resolve_expression(compiler.query)
sql, sql_params = compiler.compile(value)
sqls.append(sql)
sqls_params.extend(sql_params)
else:
_, params = self.get_db_prep_lookup(rhs, connection)
sqls, sqls_params = ['%s'] * len(params), params
return sqls, sqls_params
示例4: get_context_data
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def get_context_data(self, **kwargs):
context = super(CommentedDetailView, self).get_context_data(**kwargs)
queryset = Comment.objects.filter(hidden=False, page=self.get_comment_page())
context['has_comments'] = queryset.exists()
context['comment_lock'] = self.is_comment_locked()
queryset = queryset.select_related('author__user').defer('author__about').annotate(revisions=Count('versions'))
if self.request.user.is_authenticated:
queryset = queryset.annotate(vote_score=Coalesce(RawSQLColumn(CommentVote, 'score'), Value(0)))
profile = self.request.profile
unique_together_left_join(queryset, CommentVote, 'comment', 'voter', profile.id)
context['is_new_user'] = (not self.request.user.is_staff and
not profile.submission_set.filter(points=F('problem__points')).exists())
context['comment_list'] = queryset
context['vote_hide_threshold'] = settings.DMOJ_COMMENT_VOTE_HIDE_THRESHOLD
return context
示例5: process_rhs
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def process_rhs(self, compiler, connection):
value = self.rhs
if self.bilateral_transforms:
if self.rhs_is_direct_value():
# Do not call get_db_prep_lookup here as the value will be
# transformed before being used for lookup
value = Value(value, output_field=self.lhs.output_field)
value = self.apply_bilateral_transforms(value)
value = value.resolve_expression(compiler.query)
# Due to historical reasons there are a couple of different
# ways to produce sql here. get_compiler is likely a Query
# instance and as_sql just something with as_sql. Finally the value
# can of course be just plain Python value.
if hasattr(value, 'get_compiler'):
value = value.get_compiler(connection=connection)
if hasattr(value, 'as_sql'):
sql, params = compiler.compile(value)
return '(' + sql + ')', params
else:
return self.get_db_prep_lookup(value, connection)
示例6: batch_process_rhs
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def batch_process_rhs(self, compiler, connection, rhs=None):
if rhs is None:
rhs = self.rhs
if self.bilateral_transforms:
sqls, sqls_params = [], []
for p in rhs:
value = Value(p, output_field=self.lhs.output_field)
value = self.apply_bilateral_transforms(value)
value = value.resolve_expression(compiler.query)
sql, sql_params = compiler.compile(value)
sqls.append(sql)
sqls_params.extend(sql_params)
else:
params = self.lhs.output_field.get_db_prep_lookup(
self.lookup_name, rhs, connection, prepared=True)
sqls, sqls_params = ['%s'] * len(params), params
return sqls, sqls_params
示例7: process_rhs
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def process_rhs(self, compiler, connection):
value = self.rhs
if self.bilateral_transforms:
if self.rhs_is_direct_value():
# Do not call get_db_prep_lookup here as the value will be
# transformed before being used for lookup
value = Value(value, output_field=self.lhs.output_field)
value = self.apply_bilateral_transforms(value)
value = value.resolve_expression(compiler.query)
# Due to historical reasons there are a couple of different
# ways to produce sql here. get_compiler is likely a Query
# instance, _as_sql QuerySet and as_sql just something with
# as_sql. Finally the value can of course be just plain
# Python value.
if hasattr(value, 'get_compiler'):
value = value.get_compiler(connection=connection)
if hasattr(value, 'as_sql'):
sql, params = compiler.compile(value)
return '(' + sql + ')', params
if hasattr(value, '_as_sql'):
sql, params = value._as_sql(connection=connection)
return '(' + sql + ')', params
else:
return self.get_db_prep_lookup(value, connection)
示例8: as_postgresql
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def as_postgresql(self, compiler, connection):
geo_field = GeometryField(srid=self.srid) # Fake field to get SRID info
src_field = self.get_source_fields()[0]
geography = src_field.geography and self.srid == 4326
if geography:
# Set parameters as geography if base field is geography
for pos, expr in enumerate(
self.source_expressions[self.geom_param_pos + 1:], start=self.geom_param_pos + 1):
if isinstance(expr, GeomValue):
expr.geography = True
elif geo_field.geodetic(connection):
# Geometry fields with geodetic (lon/lat) coordinates need special distance functions
if self.spheroid:
self.function = 'ST_Distance_Spheroid' # More accurate, resource intensive
# Replace boolean param by the real spheroid of the base field
self.source_expressions[2] = Value(geo_field._spheroid)
else:
self.function = 'ST_Distance_Sphere'
return super(Distance, self).as_sql(compiler, connection)
示例9: __init__
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def __init__(self, expression, distinct=False, **extra):
if expression == '*':
expression = Value(expression)
super(Count, self).__init__(
expression, distinct='DISTINCT ' if distinct else '', output_field=IntegerField(), **extra)
示例10: coalesce
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def coalesce(self):
# null on either side results in null for expression, wrap with coalesce
expressions = [
Coalesce(expression, Value('')) for expression in self.get_source_expressions()]
self.set_source_expressions(expressions)
示例11: __init__
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def __init__(self, *expressions, **extra):
super().__init__(*expressions, **extra)
self.config = self.extra.get("config", self.config)
weight = self.extra.get("weight")
if weight is not None and not hasattr(weight, "resolve_expression"):
weight = Value(weight)
self.weight = weight
示例12: process_rhs
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def process_rhs(self, compiler, connection):
value = self.rhs
if self.bilateral_transforms:
if self.rhs_is_direct_value():
# Do not call get_db_prep_lookup here as the value will be
# transformed before being used for lookup
value = Value(value, output_field=self.lhs.output_field)
value = self.apply_bilateral_transforms(value)
value = value.resolve_expression(compiler.query)
if hasattr(value, 'as_sql'):
sql, params = compiler.compile(value)
return '(' + sql + ')', params
else:
return self.get_db_prep_lookup(value, connection)
示例13: __init__
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def __init__(self, *expressions, **extra):
super().__init__(*expressions, **extra)
self.source_expressions = [
Coalesce(expression, Value('')) for expression in self.source_expressions
]
self.config = self.extra.get('config', self.config)
weight = self.extra.get('weight')
if weight is not None and not hasattr(weight, 'resolve_expression'):
weight = Value(weight)
self.weight = weight
示例14: resolve_expression
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
resolved = super().resolve_expression(query, allow_joins, reuse, summarize, for_save)
if self.config:
if not hasattr(self.config, 'resolve_expression'):
resolved.config = Value(self.config).resolve_expression(query, allow_joins, reuse, summarize, for_save)
else:
resolved.config = self.config.resolve_expression(query, allow_joins, reuse, summarize, for_save)
return resolved
示例15: _do_search
# 需要導入模塊: from django.db.models import expressions [as 別名]
# 或者: from django.db.models.expressions import Value [as 別名]
def _do_search(self):
queryset = self.get_queryset()
if self._score_field:
queryset = queryset.annotate(**{self._score_field: Value(None, output_field=models.FloatField())})
return queryset.iterator()