Python django.db.models.expressions.Value() Examples
The following are 30
code examples of django.db.models.expressions.Value().
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: lookups.py From Hands-On-Application-Development-with-PyCharm with MIT License | 6 votes |
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
Example #2
Source File: functions.py From openhgsenti with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: lookups.py From openhgsenti with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: lookups.py From openhgsenti with Apache License 2.0 | 6 votes |
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
Example #5
Source File: lookups.py From python with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: lookups.py From python with Apache License 2.0 | 6 votes |
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
Example #7
Source File: lookups.py From python2017 with MIT License | 6 votes |
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
Example #8
Source File: lookups.py From python2017 with MIT License | 6 votes |
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)
Example #9
Source File: functions.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
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)
Example #10
Source File: search.py From contratospr-api with Apache License 2.0 | 6 votes |
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
Example #11
Source File: lookups.py From bioforum with MIT License | 6 votes |
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
Example #12
Source File: search.py From python2017 with MIT License | 5 votes |
def __init__(self, *expressions, **extra): super(SearchVector, self).__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
Example #13
Source File: aggregates.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
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)
Example #14
Source File: expressions.py From django-postgres-extra with MIT License | 5 votes |
def IsNotNone(*fields, default=None): """Selects whichever field is not None, in the specified order. Arguments: fields: The fields to attempt to get a value from, in order. default: The value to return in case all values are None. Returns: A Case-When expression that tries each field and returns the specified default value when all of them are None. """ when_clauses = [ expressions.When( ~expressions.Q(**{field: None}), then=expressions.F(field) ) for field in reversed(fields) ] return expressions.Case( *when_clauses, default=expressions.Value(default), output_field=CharField(), )
Example #15
Source File: functions.py From openhgsenti with Apache License 2.0 | 5 votes |
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: self.source_expressions.append(Value(self.spheroid)) elif geo_field.geodetic(connection): # Geometry fields with geodetic (lon/lat) coordinates need length_spheroid self.function = 'ST_Length_Spheroid' self.source_expressions.append(Value(geo_field._spheroid)) else: dim = min(f.dim for f in self.get_source_fields() if f) if dim > 2: self.function = connection.ops.length3d return super(Length, self).as_sql(compiler, connection)
Example #16
Source File: functions.py From openhgsenti with Apache License 2.0 | 5 votes |
def as_sqlite(self, compiler, connection): func_name = connection.ops.spatial_function_name(self.name) if func_name == 'ST_Translate' and len(self.source_expressions) < 4: # Always provide the z parameter for ST_Translate (Spatialite >= 3.1) self.source_expressions.append(Value(0)) elif func_name == 'ShiftCoords' and len(self.source_expressions) > 3: raise ValueError("This version of Spatialite doesn't support 3D") return super(Translate, self).as_sqlite(compiler, connection)
Example #17
Source File: test_cast.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_cast_from_value(self): numbers = Author.objects.annotate(cast_integer=Cast(Value('0'), models.IntegerField())) self.assertEqual(numbers.get().cast_integer, 0)
Example #18
Source File: test_aggregates.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_correct_source_expressions(self): func = StatAggregate(x='test', y=13) self.assertIsInstance(func.source_expressions[0], Value) self.assertIsInstance(func.source_expressions[1], F)
Example #19
Source File: test_cast.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_cast_from_value(self): numbers = Author.objects.annotate(cast_integer=Cast(Value('0'), models.IntegerField())) self.assertEqual(numbers.get().cast_integer, 0)
Example #20
Source File: search.py From python2017 with MIT License | 5 votes |
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False): resolved = super(SearchVector, self).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
Example #21
Source File: search.py From python2017 with MIT License | 5 votes |
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False): resolved = super(SearchQuery, self).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
Example #22
Source File: search.py From python2017 with MIT License | 5 votes |
def __init__(self, expression, string, **extra): if not hasattr(string, 'resolve_expression'): string = Value(string) super(TrigramBase, self).__init__(expression, string, output_field=FloatField(), **extra)
Example #23
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 #24
Source File: test_cast.py From django-sqlserver with MIT License | 5 votes |
def test_cast_from_value(self): numbers = Author.objects.annotate(cast_integer=Cast(Value('0'), models.IntegerField())) self.assertEqual(numbers.get().cast_integer, 0)
Example #25
Source File: 0001_initial.py From Inboxen with GNU Affero General Public License v3.0 | 5 votes |
def combine_index(field_a, field_b): vector = SearchVector(Value(field_a, output_field=models.TextField()), config=settings.SEARCH_CONFIG, weight="A") + \ SearchVector(Value(field_b, output_field=models.TextField()), config=settings.SEARCH_CONFIG, weight="B") return vector
Example #26
Source File: models.py From Inboxen with GNU Affero General Public License v3.0 | 5 votes |
def update_search(self): vectors = SearchVector(Value("", output_field=models.TextField()), config=settings.SEARCH_CONFIG) for weight in ["a", "b", "c", "d"]: if hasattr(self, "index_search_{}".format(weight)): content = getattr(self, "index_search_{}".format(weight))() vectors = vectors + SearchVector(Value(content, output_field=models.TextField()), config=settings.SEARCH_CONFIG, weight=weight.upper()) self.search_tsv = vectors
Example #27
Source File: test_aggregates.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_correct_source_expressions(self): func = StatAggregate(x='test', y=13) self.assertIsInstance(func.source_expressions[0], Value) self.assertIsInstance(func.source_expressions[1], F)
Example #28
Source File: query.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
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
Example #29
Source File: functions.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
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)
Example #30
Source File: search.py From contratospr-api with Apache License 2.0 | 5 votes |
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